作者:超级无敌智慧星在路上 | 来源:互联网 | 2023-09-18 17:32
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 个解决方案
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,这可能不合理,我上面所说的可以通过方法调配来完成:只有当选择器名称与您的表达式不匹配时,才调用原始实现,否则什么都不做。