Article

Understanding Setup and Teardown for Test Methods

Prepare initial state before tests run and perform cleanup after tests complete.

Overview

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.

Listing 1

Example test case with setup and teardown methods and blocks

class SetUpAndTearDownExampleTestCase: XCTestCase {
    
    override class func setUp() { // 1.
        super.setUp()
        // This is the setUp() class method.
        // It is called before the first test method begins.
        // Set up any overall initial state here.
    }
    
    override func setUp() { // 2.
        super.setUp()
        // This is the setUp() instance method.
        // It is called before each test method begins.
        // Set up any per-test state here.
    }
    
    func testMethod1() { // 3.
        // This is the first test method.
        // Your testing code goes here.
        addTeardownBlock { // 4.
            // Called when testMethod1() ends.
        }
    }
    
    func testMethod2() { // 5.
        // This is the second test method.
        // Your testing code goes here.
        addTeardownBlock { // 6.
            // Called when testMethod2() ends.
        }
        addTeardownBlock { // 7.
            // Called when testMethod2() ends.
        }
    }
    
    override func tearDown() { // 8.
        // This is the tearDown() instance method.
        // It is called after each test method completes.
        // Perform any per-test cleanup here.
        super.tearDown()
    }
    
    override class func tearDown() { // 9.
        // This is the tearDown() class method.
        // It is called after all test methods complete.
        // Perform any overall cleanup here.
        super.tearDown()
    }
    
}

Figure 1

Order of execution for the example test case in Listing 1.

Schematic representation of the order in which setup, test, and teardown blocks and methods are called for 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.

See Also

Customizing Test Setup and Teardown

+ setUp

Provides an opportunity to customize initial state before a test case begins.

- addTeardownBlock:

Registers a block of teardown code to be run after the current test method ends.

+ tearDown

Provides an opportunity to perform cleanup after a test case ends.