All About setUp() And tearDown()

Samruddhi Jadhav
3 min readNov 14, 2018

--

When Xcode runs tests, it invokes each test method independently. Therefore each method must prepare and cleanup all the allocations. For that it has two important methods, setUp()and tearDown().

  1. setUp() — This method is called before the invocation of each test method in the given class.
  2. tearDown() — This method is called after the invocation of each test method in given class.

Each class testing starts with class setUp()method execution.

Then for each test method,

1. A new instance of class is allocated.

2. Its instance setUp() method is executed.

3. Runs the test.

4. Instance tearDown()method is executed.

This sequence repeats for all tests methods in the class.

After last test method, when instance tearDown() has been run, Xcode executes the class teardown method.

You can customize setUp()and teardown() in 5 different ways,

1. Override setUp() class method to setup initial state for all test methods.

2. Override setUp() instance method to reset initial state before each test method is executed.

3. You can have self-contained blocks of teardown code with the addTearDownBlock(_:) method during a test method’s execution.

4. Override tearDown() instance method to clean after each test method executes.

5. Override tearDown() class method to clean after all test methods execute.

Let’s check the execution of below tests,

//Source: developer.apple.comclass 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()
}

}
Order of Execution, Source: developer.apple.com

We can see here that teardown blocks registered during test method’s execution are run after that test method ends and before the teardown instance method is called. Also Teardown blocks run in last-in-first out basis.

NOTE:

The setUp() and tearDown() class methods are defined on XCTestCase whereas setUp() and tearDown() instance methods are defined on its base, XCTest.

Teardown blocks and overridden tearDown() methods are called regardless of whether a test method passes or fails, even if test case’s continueAfterFailure is set to false.

TIP:

You can include test assertions in test cases setUp() and tearDown() instance methods.

However you can’t include test assertions in test case’s setUp() and tearDown() class methods. Test assertions require class instance, which doesn’t exist within the scope of class method.

Thanks For Reading 🎾

--

--