I’ve been adding some Unit Tests to an iOS App lately, which had both Test Host
and Bundle Loader
set for the Unit-Testing target. Now executing the unit tests caused the iOS App to launch, and the launch itself interfered with my unit-tests (e.g. real network calls which interfere with your mocked network calls).
I needed a way to stop the launch if we are running in a unit testing scenario. So how do you detect if Unit-Tests are being run in Objective-C ? Turns out it’s extremly simple:
BOOL runningTests = NSClassFromString(@"SenTestCase") != nil;
That’s all you need to check if you are running Unit-Tests - an additional return
at the right place and your app
stops dead when executing unit tests.
There are a few assumptions about your test targets setup:
Test Host
is set to your iOS AppBundle Loader
is set to your iOS App
Happy Hacking :)