Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Conditional unit testing based on iOS version

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.

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

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?

like image 830
hpique Avatar asked Oct 15 '13 10:10

hpique


People also ask

What is unit testing in iOS?

A unit test is a function you write that tests something about your app. A good unit test is small. It tests just one thing in isolation. For example, if your app adds up the total amount of time your user spent doing something, you might write a test to check if this total is correct.

What is XCTest framework in iOS?

Use the XCTest framework to write unit tests for your Xcode projects that integrate seamlessly with Xcode's testing workflow. Tests assert that certain conditions are satisfied during code execution, and record test failures (with optional messages) if those conditions aren't satisfied.

How do I run a test case in iOS Swift?

Writing Unit Test Cases in Swift With an ExampleUnit test cases run with the testing target. Always check if you have a unit test target or not, if not, then add it. Then under this target, you need to create a new test case file. You will use this new file for writing test cases.

How do I run XCTest in Xcode?

To run your app's XCTests on Test Lab devices, build it for testing on a Generic iOS Device: From the device dropdown at the top of your Xcode workspace window, select Generic iOS Device. In the macOS menu bar, select Product > Build For > Testing.


2 Answers

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.

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.

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.

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.

EDIT:

Actually, things could be easier that what I thought in the first place. Now I am going a bit 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).

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.

like image 115
sergio Avatar answered Oct 02 '22 03:10

sergio


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

[view respondsToSelector:@selector(snapshotViewAfterScreenUpdates:)]

or

[UIView instancesRespondToSelector:@selector(snapshotViewAfterScreenUpdates:)]
like image 20
Guy Kogus Avatar answered Oct 02 '22 02:10

Guy Kogus