We want to run tests in a clean environment. It’s important that we control any surrounding state that may affect test outcomes. As we discussed in Chapter 2, Manage Your Test Life Cycles, this is part of having isolated tests, which is one of the FIRST properties of unit tests.[13]
But unit tests for an iOS application won’t have a clean environment—unless we take steps to create it. We saw a hint of this in Chapter 3, Measure Code Coverage and Add Tests when we saw the non-zero code coverage for the app delegate and view controller. Unless we’re careful, the app will continue to use its normal launch sequence, even for test runs. This could create unwanted side effects that affect test runs. It can also slow down the test feedback loop. Tests don’t start running until the app finishes launching.
In this chapter, we’ll go over how to use a separate app delegate when running tests. By keeping your regular app delegate out of the way, you’ll have greater control over your test environment. This will also make your code coverage results a little more accurate.