Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Understanding XCTest framework

Eeb061c8b2816b771920da1b3e7904a3?s=47 Swift India
November 16, 2019

Understanding XCTest framework

Eeb061c8b2816b771920da1b3e7904a3?s=128

Swift India

November 16, 2019
Tweet

Transcript

  1. Understanding XCTest framework Ratikanta Patra iOS Engineer

  2. 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

  3. 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
  4. 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)
  5. XCTest - Object life cycle • has to be released

    explicitly
 
 • create in setUp - cleanup in tearDown
 
 • might affect result of other tests if not released
  6. 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
  7. 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() } }
  8. 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
  9. 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
  10. 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
  11. 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() }
  12. 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.
  13. 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
  14. References • https://www.essentialdeveloper.com/ • https://developer.apple.com/documentation/xctest/xctestcase/ understanding_setup_and_teardown_for_test_methods • https://qualitycoding.org/xctestcase-teardown/

  15. Questions?

  16. Thank you!