hakonbogen hakonbogen - 2 months ago 15
Swift Question

How to let the app know if its running Unit tests in a pure Swift project?

One annouting thing when running tests in XCode 6.1 is that the entire app has to run and launch its storyboard and root viewcontroller. In my app this runs some server calls that fetch api data, and i don't want the app to do this behaviour when its running its tests.

Witch preprocessor macros gone, whats the best for my project to be aware that it was launched running tests and not ordinary launch? I run them normally with CMD+U and on a bot.

pseudo code would be

// Appdelegate.swift

if runningTests() {
} else {
// do ordinary api calls


Instead of checking if the tests are running to avoid side-effects, you could run the tests without the host app itself. Go to Project Settings -> select the test target -> General -> Testing -> Host Application -> select 'None'. Just remember to include all files you need to run the tests, as well as libraries normally included by the Host app target.

enter image description here