3

I'm going through a great book learning about test-driven development in Swift. My ultimate goal is to get a better understanding of OOP architecture. As I'm reading the book, a section early on states that the setUp() method is fired before each test method which I understand does the setup of objects to run the test for a pass or fail result. What I'm unsure of is how is this even possible I'm guessing from an architecture stand-point? How was Apple able to make a class that has a method which is fired before every other method in the class?

Here is some sample code:

import XCTest
@testable import FirstDemo

class FirstDemoTests: XCTestCase {

    override func setUp() {
        super.setUp()
        // Put setup code here. This method is called before the invocation of each test method in the class.
    }

    override func tearDown() {
        // Put teardown code here. This method is called after the invocation of each test method in the class.
        super.tearDown()
    }

    func testExample() {
        // This is an example of a functional test case.
        // Use XCTAssert and related functions to verify your tests produce the correct results.
    }

    func testPerformanceExample() {
        // This is an example of a performance test case.
        self.measure {
            // Put the code you want to measure the time of here.
        }
    }

}
Laurence Wingo
  • 3,912
  • 7
  • 33
  • 61

3 Answers3

4

I think XCTest class has a lifecycle just like UIViewController for example.

Same as viewDidLoad() called after init(coder:) when the view is loaded into memory, setUp() method is also called after the test class is loaded (once during the life of the object).

You can also investigate native and third party test frameworks source code on github:

https://github.com/apple/swift-corelibs-xctest

https://github.com/google/googletest

Oleh Zayats
  • 2,423
  • 1
  • 16
  • 26
  • thank you and if this is the case, then how do I go about understanding how the methods of UIViewController are fired? For example I believe the ViewDidLoad() is fired automatically by the runtime environment. But what do we mean by "automatically", I'm thinking something somewhere is programmed to call this method and I'd like to know where that is to learn more about it. I hope this makes sense – Laurence Wingo Apr 04 '17 at 14:06
2

You are Subclassing a Class called XCTestCase. Usally Testframework introspects Classes in which Test-Methods are defined and run them in a particular order.

Anyway, im not 100% sure if the XCTest is working this way, but you could try to have a look at the source code and try to dig deeper there:

https://github.com/apple/swift-corelibs-xctest

Kris
  • 512
  • 7
  • 16
  • thank you this is a good point because I forgot Swift is open source but I find navigating github repos somewhat confusing as far as how to find the area I'm interested in. Any tips on going about this? – Laurence Wingo Apr 04 '17 at 14:01
  • 1
    I wouldn't try to understand the whole source, just from reading it in git... I would try to check it out and build it. After building, try to reference it another project, write some silly test, just to make sure it works. Then play with. Change the source code a bit, break it, introduce some bugs. Recompile everything and see what happens. – Kris Apr 04 '17 at 14:05
2

For each test to be tested: setup() is called, then the actual test, then teardown().

Then again setup another test from that class and tearDown again...until all** tests of your test class get ran.

The sequence of the lines won't affect which test gets called first.

The reason that it's made this way is because 2 tests should be 100% independent. You don't want testExample do something to the object/sut/system_under_test (mutate its state) you're performing your tests on but, not undo what it's done. Otherwise your testPerformanceExample would be loaded from a state you weren't expecting.

mfaani
  • 33,269
  • 19
  • 164
  • 293