How to Do XCTestCase tearDown Wrong (and Right)
Jon Reid (via Natasha Murashev):
In other words, it builds up the entire set of
XCTestCase
instances before running a single test. […]setUp
andtearDown
were invented because the entire collection of test cases is created up front. They provide the hooks to manage object life cycle in tests.This has two important implications:
- Anything automatically created as part of the
XCTestCase
’s initialization will exist too soon.- Anything not released in the
tearDown
will continue to exist, even while other tests run.Think of any object that alters global state, and shudder.
Which is too bad because the Swift code isn’t as clean when everything has to be optional, since it isn’t initialized in init
.