Prepare initial state before tests run and perform cleanup after tests complete.
You can customize the setup and teardown of test state in five different ways.
Override the setUp class method to set up initial state for all test methods.
Override the setUp instance method to reset initial state before each test method is run.
Register self-contained blocks of teardown code with the addTeardownBlock: method during a test method's execution.
Override the tearDown instance method to perform cleanup after each test method completes.
Override the tearDown class method to perform final cleanup after all test methods complete.
Teardown blocks registered during a test method's execution are run after that test method ends, before the tearDown instance method is called. Teardown blocks are run on a last-in, first-out basis. Registered teardown blocks and overridden tearDown methods are called regardless of whether a test method passes or fails, even if the test case's continueAfterFailure property is set to NO.
Listing 1 and show the order of execution for the setup and teardown of test methods in a test case.
In the execution flow for testMethod2() above, the second teardown block (numbered 7 in the code listing) is executed before the first teardown block (numbered 6 in the code listing). This is because teardown blocks are executed in last-in, first-out (LIFO) order, so the last block registered is the first block executed.