Slide 1

Slide 1 text

Understanding XCTest framework Ratikanta Patra iOS Engineer

Slide 2

Slide 2 text

Agenda • Understanding how XCTests work • Life cycle of an object in XCTest • Efficient use of setUp and tearDown • Using tearDown blocks • Testing memory leaks using XCTests


Slide 3

Slide 3 text

XCTestCase subclasses Test methods e.g func testValue() { // testing logic } testMethod1() —XCTestCaseObj1 testMethod2() —XCTestCaseObj2 ———————————————— - no arguments, 
 - no return types, 
 - begins with word “test” Collection How XCTest Works

Slide 4

Slide 4 text

class XCTestDemoIntegerTests: XCTestCase { var counter: Int = 1 func testIncrement() { counter += 1 XCTAssertEqual(counter, 2) } func testDecrement() { counter -= 1 XCTAssertEqual(counter, 1) } } class XCTestDemoIntegerTests: XCTestCase { var counter: Int = 1 func testIncrement() { counter += 1 XCTAssertEqual(counter, 2) } func testDecrement() { counter -= 1 } } Example XCTAssertEqual(counter, 0)

Slide 5

Slide 5 text

XCTest - Object life cycle • has to be released explicitly
 
 • create in setUp - cleanup in tearDown
 
 • might affect result of other tests if not released

Slide 6

Slide 6 text

class XCTestObjectLifeCycleTest: XCTestCase { private var storageManager: DataStorageManager! override func setUp() { super.setUp() storageManager = DataStorageManager() storageManager.store(data: (["someKey": "someData"])) } func testDataStoreManager() { XCTAssertEqual(storageManager.fetchStoredData().count, 1) } } Example

Slide 7

Slide 7 text

class DataStorageManager { init() { prepare() } private func prepare() { // setup DB connection // write to UserDefaults } /// Stores the given data in file system/DB or some other storage method func store(data: Any) { } func fetchData() -> [Any] { return ["some data"] } func clean() { // close db connection // remove files // clear UserDefaults } deinit { clean() } }

Slide 8

Slide 8 text

class XCTestObjectLifeCycleTest: XCTestCase { private var storageManager: DataStorageManager! override func setUp() { super.setUp() storageManager = DataStorageManager() storageManager.store(data: (["someKey": "someData"])) } func testDataStoreManager() { XCTAssertEqual(storageManager.fetchStoredData().count, 1) } override func tearDown() { super.tearDown() } } storageManager = nil Example

Slide 9

Slide 9 text

setUp() class method scope - global to all tests setup initial state for all tests setUp() instance method scope - local to each test setup initial state before each test begins tearDown() class method scope - global to all tests final clean up for all tests tearDown() instance method scope - global to all tests clean up before each test ends setUp and tearDown

Slide 10

Slide 10 text

tearDown blocks • Registers a block to be invoked when test method ends • Always invoked before tearDown() instance method • Can add multiple tearDown blocks, executed in LIFO order • Invoked on the main thread

Slide 11

Slide 11 text

class XCTestTearDownBlockTest: XCTestCase { func testStorageManager() { let manager = storageManagerForData(["someKey": "someData"]) let storedData = manager.fetchStoredData() XCTAssertTrue(storedData.count == 1) } private func storageManagerForData(_ data: Any) -> DataStorageManager { let storageManager = DataStorageManager() storageManager.store(data: data) return storageManager } } Example addTeardownBlock { storageManager.clean() }

Slide 12

Slide 12 text

setUp() class method setUp() instance method tearDownBlock n tearDownBlock 1 tearDown() instance method tearDown() class method tearDownBlock n-1 Order of Execution testMethod1() repeats for each test method 1. 2. 3. 4. 5. 6.

Slide 13

Slide 13 text

Testing memory leaks • We can write test cases to check memory leaks by keeping a weak reference to the object in our test class • Check that the weak reference in nil the tearDown() method • Demo • Source: https://www.essentialdeveloper.com

Slide 14

Slide 14 text

References • https://www.essentialdeveloper.com/ • https://developer.apple.com/documentation/xctest/xctestcase/ understanding_setup_and_teardown_for_test_methods • https://qualitycoding.org/xctestcase-teardown/

Slide 15

Slide 15 text

Questions?

Slide 16

Slide 16 text

Thank you!