热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

基于iOS版本的条件单元测试-ConditionalunittestingbasedoniOSversion

MyunittestsuitecontainsafewtestcasesthatareonlyapplicableforthelatestiOSversion.Ex

My unit test suite contains a few test cases that are only applicable for the latest iOS version. Expectedly, these tests fail when running the suite on an older iOS version.

我的单元测试套件包含一些仅适用于最新iOS版本的测试用例。预计,在较旧的iOS版本上运行套件时,这些测试会失败。

Is there a standard way to indicate that these test cases should run on a specific iOS version only?

是否有标准方法来指示这些测试用例应仅在特定的iOS版本上运行?

One option I thought about:

我想到的一个选择:

- (void) testSomethingThatOnlyWorksOniOS7
{
    const BOOL iOS7OrHigher = floor(NSFoundationVersionNumber) > NSFoundationVersionNumber_iOS_6_1;
    if (!iOS7OrHigher) return;

    // Actual test goes here
}

Alternatively, could I tell SenTestCase to skip certain tests dynamically?

或者,我可以告诉SenTestCase动态跳过某些测试吗?

4 个解决方案

#1


4  

Your approach seems fine to me.

你的方法对我来说似乎很好。

Generally speaking, I do not think that there is something like a "standard way" to selectively execute unit tests based on iOS version.

一般来说,我认为没有像“标准方式”那样有选择地执行基于iOS版本的单元测试。

On the one hand, your approach is the same that can be used to implement one feature selectively or in different ways according to iOS version. Possibly you already know about this discussion on CocoaWithLove: Tips and Tricks from conditional.... It's pretty old, but the approaches described here to hold still.

一方面,根据iOS版本,您的方法可以用于有选择地或以不同方式实现一个功能。可能你已经知道关于CocoaWithLove的讨论了:有条件的提示和技巧......它已经很老了,但是这里所描述的方法仍然存在。

You do not specify the way your unit tests are executed, but the real cool way to handle this, IMO, would be to be able to specify which tests to execute outside of the implementation, so that you state which ones are for iOS7 only and do not pollute your test implementation.

你没有指定单元测试的执行方式,但处理这个问题的真正酷的方法是IMO,它能够指定在实现之外执行哪些测试,以便你说明哪些只适用于iOS7,不要污染你的测试实现。

This could done, e.g., through a version number associated to each test; before calling the unit test implementation you check the version number in the function calling, e.g., testSomethingThatOnlyWorksOniOS7.

这可以通过例如与每个测试相关联的版本号来完成;在调用单元测试实现之前,请检查函数调用中的版本号,例如testSomethingThatOnlyWorksOniOS7。

EDIT:

编辑:

Actually, things could be easier that what I thought in the first place. Now I am going a bit hacky...

实际上,事情可能比我想到的更容易。现在我有点hacky ......

You could modify the place in OCUnit where the actual test method call is done (no idea about this, sorry, but I do not think it should be hard to find out...) so that it checks on the selector name: you convert the selector name into a string (NSStringFromSelector) and if it matches some regular expression, you take some specific branch (which would simply be in your case, ignoring that test).

您可以在OCUnit中修改实际测试方法调用的地方(不知道这个,对不起,但我不认为应该很难找到...)以便它检查选择器名称:您转换将选择器名称转换为字符串(NSStringFromSelector),如果它匹配某个正则表达式,则需要一些特定的分支(在您的情况下,这将忽略该测试)。

If you are worried by modifying OCUnit, which might not be sensible, what I said above could be done through method swizzling: only if the selector name does not match your expression, you call the original implementation, otherwise do nothing.

如果您担心修改OCUnit,这可能不合理,我上面所说的可以通过方法调配来完成:只有当选择器名称与您的表达式不匹配时,才调用原始实现,否则什么都不做。

#2


3  

The best way is to run respondsToSelector: on a method you know is only available on iOS 7. E.g.

最好的方法是运行respondsToSelector:在一个你知道的方法只能在iOS 7上运行。

[view respondsToSelector:@selector(snapshotViewAfterScreenUpdates:)]

or

要么

[UIView instancesRespondToSelector:@selector(snapshotViewAfterScreenUpdates:)]

#3


1  

There is not a standard way of doing this, and generally speaking testing is not something the iOS community talks too much about. The way I would approach this is by putting specific iOS7 tests on a test target on its own and then basically decide to run that suite accordingly

没有一种标准的方法可以做到这一点,一般而言,测试不是iOS社区过多谈论的内容。我接近这个的方法是将特定的iOS7测试单独放在测试目标上,然后基本上决定相应地运行该套件

#4


0  

I tried playing around with OCUnit as @sergio suggested but I couldn't get far away. So far I'm annotating version-specific tests with a macro. It looks like this:

我试着和OCUnit一起玩@sergio建议,但我无法远离。到目前为止,我正在使用宏来注释特定于版本的测试。它看起来像这样:

#define SKIP_IF_VERSION(v) if (floor(NSFoundationVersionNumber) <= v) return;

- (void)testSomethingThatOnlyWorksOniOS7
{ SKIP_IF_VERSION(NSFoundationVersionNumber_iOS_6_1)
// Test goes here
}

What I don't like about this approach is that the test runs anyway (and doesn't do anything). I would prefer the test to be skipped entirely.

我不喜欢这种方法是测试运行无论如何(并没有做任何事情)。我宁愿完全跳过测试。


推荐阅读
  • Linux重启网络命令实例及关机和重启示例教程
    本文介绍了Linux系统中重启网络命令的实例,以及使用不同方式关机和重启系统的示例教程。包括使用图形界面和控制台访问系统的方法,以及使用shutdown命令进行系统关机和重启的句法和用法。 ... [详细]
  • 本文介绍了九度OnlineJudge中的1002题目“Grading”的解决方法。该题目要求设计一个公平的评分过程,将每个考题分配给3个独立的专家,如果他们的评分不一致,则需要请一位裁判做出最终决定。文章详细描述了评分规则,并给出了解决该问题的程序。 ... [详细]
  • 本文主要解析了Open judge C16H问题中涉及到的Magical Balls的快速幂和逆元算法,并给出了问题的解析和解决方法。详细介绍了问题的背景和规则,并给出了相应的算法解析和实现步骤。通过本文的解析,读者可以更好地理解和解决Open judge C16H问题中的Magical Balls部分。 ... [详细]
  • 本文讨论了使用差分约束系统求解House Man跳跃问题的思路与方法。给定一组不同高度,要求从最低点跳跃到最高点,每次跳跃的距离不超过D,并且不能改变给定的顺序。通过建立差分约束系统,将问题转化为图的建立和查询距离的问题。文章详细介绍了建立约束条件的方法,并使用SPFA算法判环并输出结果。同时还讨论了建边方向和跳跃顺序的关系。 ... [详细]
  • Python正则表达式学习记录及常用方法
    本文记录了学习Python正则表达式的过程,介绍了re模块的常用方法re.search,并解释了rawstring的作用。正则表达式是一种方便检查字符串匹配模式的工具,通过本文的学习可以掌握Python中使用正则表达式的基本方法。 ... [详细]
  • CSS3选择器的使用方法详解,提高Web开发效率和精准度
    本文详细介绍了CSS3新增的选择器方法,包括属性选择器的使用。通过CSS3选择器,可以提高Web开发的效率和精准度,使得查找元素更加方便和快捷。同时,本文还对属性选择器的各种用法进行了详细解释,并给出了相应的代码示例。通过学习本文,读者可以更好地掌握CSS3选择器的使用方法,提升自己的Web开发能力。 ... [详细]
  • 本文讨论了如何优化解决hdu 1003 java题目的动态规划方法,通过分析加法规则和最大和的性质,提出了一种优化的思路。具体方法是,当从1加到n为负时,即sum(1,n)sum(n,s),可以继续加法计算。同时,还考虑了两种特殊情况:都是负数的情况和有0的情况。最后,通过使用Scanner类来获取输入数据。 ... [详细]
  • 本文讨论了一个关于cuowu类的问题,作者在使用cuowu类时遇到了错误提示和使用AdjustmentListener的问题。文章提供了16个解决方案,并给出了两个可能导致错误的原因。 ... [详细]
  • 本文介绍了P1651题目的描述和要求,以及计算能搭建的塔的最大高度的方法。通过动态规划和状压技术,将问题转化为求解差值的问题,并定义了相应的状态。最终得出了计算最大高度的解法。 ... [详细]
  • 本文介绍了游标的使用方法,并以一个水果供应商数据库为例进行了说明。首先创建了一个名为fruits的表,包含了水果的id、供应商id、名称和价格等字段。然后使用游标查询了水果的名称和价格,并将结果输出。最后对游标进行了关闭操作。通过本文可以了解到游标在数据库操作中的应用。 ... [详细]
  • ALTERTABLE通过更改、添加、除去列和约束,或者通过启用或禁用约束和触发器来更改表的定义。语法ALTERTABLEtable{[ALTERCOLUMNcolu ... [详细]
  • Java学习笔记之面向对象编程(OOP)
    本文介绍了Java学习笔记中的面向对象编程(OOP)内容,包括OOP的三大特性(封装、继承、多态)和五大原则(单一职责原则、开放封闭原则、里式替换原则、依赖倒置原则)。通过学习OOP,可以提高代码复用性、拓展性和安全性。 ... [详细]
  • 本文讨论了clone的fork与pthread_create创建线程的不同之处。进程是一个指令执行流及其执行环境,其执行环境是一个系统资源的集合。在调用系统调用fork创建一个进程时,子进程只是完全复制父进程的资源,这样得到的子进程独立于父进程,具有良好的并发性。但是二者之间的通讯需要通过专门的通讯机制,另外通过fork创建子进程系统开销很大。因此,在某些情况下,使用clone或pthread_create创建线程可能更加高效。 ... [详细]
  • 本文介绍了最长上升子序列问题的一个变种解法,通过记录拐点的位置,将问题拆分为左右两个LIS问题。详细讲解了算法的实现过程,并给出了相应的代码。 ... [详细]
  • 预备知识可参考我整理的博客Windows编程之线程:https:www.cnblogs.comZhuSenlinp16662075.htmlWindows编程之线程同步:https ... [详细]
author-avatar
超级无敌智慧星在路上
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有