Conditional unit testing based on iOS version

My unit test package contains several test cases that apply only to the latest version of iOS. These tests are expected to fail when launching the package on an earlier version of iOS.

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

One of the options I was thinking about:

- (void) testSomethingThatOnlyWorksOniOS7 { const BOOL iOS7OrHigher = floor(NSFoundationVersionNumber) > NSFoundationVersionNumber_iOS_6_1; if (!iOS7OrHigher) return; // Actual test goes here } 

Alternatively, can I tell SenTestCase skip some tests dynamically?

+6
source share
4 answers

Your approach seems wonderful to me.

Generally speaking, I do not think that there is something like a “standard way” for the selective execution of unit tests based on the iOS version.

On the one hand, your approach is the same that can be used to implement one function selectively or differently according to the version of iOS. You may already know about this discussion at CocoaWithLove: Tips and tricks from the conditional .... This is pretty old, but the approaches described here remain.

You won’t indicate how your unit tests are performed, but the real cool way to handle this, IMO, would be to determine which tests to run outside of the implementation so that you specify which ones are for iOS7 and not pollute your test implementation.

This can be done, for example, through the version number associated with each test; Before calling the unit test implementation, you check the version number in the function call, for example, testSomethingThatOnlyWorksOniOS7 .

EDIT:

In fact, everything can be simpler than I thought in the first place. Now I’ve hacked a bit ...

You can change the place in OCUnit where the actual test method call is made (I don’t know about this, sorry, but I don’t think it should be hard to find ...) so that it checks the selector name: you convert the selector name to a string ( NSStringFromSelector ), and if it matches some regular expression, you take some specific branch (which will simply be in your case, ignoring this test).

If you are concerned about changing OCUnit, which might be unreasonable, what I said above could be done using the swizzling method: only if the selector name does not match your expression, you call the original implementation, otherwise you do nothing.

+4
source

The best way is to run respondsToSelector: according to a method that, as you know, is only available on iOS 7. For example

 [view respondsToSelector:@selector(snapshotViewAfterScreenUpdates:)] 

or

 [UIView instancesRespondToSelector:@selector(snapshotViewAfterScreenUpdates:)] 
+3
source

There is no standard way to do this, and generally speaking, testing is not something the iOS community is talking too much about. The way I would like to approach this is to put the specific iOS7 tests into the test target myself, and then basically decide to run this package accordingly

+1
source

I tried to play with OCUnit, as @sergio suggested, but I couldn't go far. So far I am commenting on the tests related to the version, with the macro. It looks like this:

 #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 works anyway (and does nothing). I would prefer the test to be skipped completely.

0
source

Source: https://habr.com/ru/post/955973/


All Articles