I would like to know if there is any way to tell Xcode to run unit tests in a specified order. I mean not in a same XCTestCase class file, but between all the class file.
For example I want to run the SitchozrSDKSessionTest before running SitchozrSDKMessageTest.
I looked over few threads on stack or on Apple documentation and I haven't found something helpful.
Thanks for your help.
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.
Use the XCTest framework to write unit tests for your Xcode projects that integrate seamlessly with Xcode's testing workflow.
To add a unit test target to an existing Xcode project, choose File > New > Target. Select your app platform (iOS, macOS, watchOS, tvOS) from the top of the New Target Assistant. Select the Unit Testing Bundle target from the list of targets.
It's all sorted alphabetically (classes and methods). So when You need some tests running last, just change the name of Class or Method (for (nasty) example by prefixing 'z_').
So...You have Class names:
MyAppTest1 -testMethodA -testMethodB MyAppTest2 -testMethodC -testMethodD
and they run in this order. If you need to run MyAppTest1 as second, just rename so it's name is alphabetically after MyAppTest2 (z_MyAppTest1) and it will run (same for method):
MyAppTest2 -a_testMethodD -testMethodC z_MyAppTest1 -testMethodA -testMethodB
Also please take the naming as example :)
It is true that currently (as of Xcode 7), XCTestCase methods are run in alphabetical order, so you can force an order for them by naming them cleverly. However, this is an implementation detail and seems like it could change.
A (hopefully) less fragile way to do this is to override +[XCTestCase testInvocations]
and return your own NSInvocation
objects in the order you want the tests run.
Something like:
+ (NSArray *)testInvocations { NSArray *selectorStrings = @[@"testFirstThing", @"testSecondThing", @"testAnotherThing", @"testLastThing"]; NSMutableArray *result = [NSMutableArray array]; for (NSString *selectorString in selectorStrings) { SEL selector = NSSelectorFromString(selectorString); NSMethodSignature *methodSignature = [self instanceMethodSignatureForSelector:selector]; NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:methodSignature]; invocation.selector = selector; [result addObject:invocation]; } return result; }
Of course, the downside here is that you have to manually add each test method to this instead of them being picked up automatically. There are a few ways to improve this situation. If you only need to order some of your test methods, not all of them, in your override of +testInvocations
, you could call through to super, filter out those methods that you've manually ordered, then tack the rest on to the end of the array you return. If you need to order all the test methods, you could still get the result of calling through to super and verify that all of the automatically picked up methods are covered by your manually created, ordered result. If not, you could assert, causing a failure if you've forgotten to add any methods.
I'm leaving aside the discussion of whether it's "correct" to write tests that must run in a certain order. I think there are rare scenarios where that makes sense, but others may disagree.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With