Liam SY Kim
by Liam SY Kim
4 min read

Categories

I have heard many times about importance of test code. However, I have no experience of managing big size app, it is hard to deal with.

I have no idea until now when the company requires to write test code on their recruiting assignment.

Anyway.. let’s dive in!

Concepts

Unit test is a test code which provides whether source code is excuted as intended or not.

In other words, Unit test is writing test case for each methods.

Thanks to unit test, developer can find out bugs fast, and it can be solved.

How to make Unit test in Xcode

For proceeding unit test, generate test target first, and then make connection between current project and unit test.

There are two ways to make unit test.

Include unit test When the project is generating.

When you generate project, Include Unit Tests is one of options.

It automatically generates Unit tests in project.

Include unit test after generating project.

If you didn’t generate unit test at first like above, you can create and include test code on your project.

You can add it to the sixth item from the left of the Navigator first and then press the + button at the bottom.

If you create a unit test target, you need to select the project you want to test in the Target to be Tested section as follows.

After that, you must finally include the test target as target membership in the files of the project under test. This is because the test target can access the module under test.

Life cycle of unit test

After generating test target, test class would be generated like as below. these methods are excuted in order.

Before explaining ordering of methods, I will let you know the role of each methods

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()
    }
    
}

setUp()

Provides an opportunity to reset state before each test method in a test case is called.

When setUp() is useful?

We can generate new instance for variable. It is same meaning with initiating from class.

tearDown()

Provides an opportunity to perform clean up after each test method in a test case ends

When tearDown() is useful?

We allocated some data from setUp() function. Some of things should be deallocated.

Transaction callback, file closing, removing element in list.

Both setUp() and tearDown() is called only once for each test methods.

testMethod 1, 2

Set test cases which you want to test. If you use XCTAssert() method here, it supports whether it is true or not.

Simply now, we know the role of methods. I think you could predict the ordering like as below.

Test Example

At first, difficult things about unit test is not about concepts but finding the way how to use.

Therefore, let us know the step of unit test by example.

I made person example. This example should check whether each person is student or not. If age of person is lower than 20, (s)he is student.

Here is struct of person.

struct Person {
    let personName: String
    let age: Int
    var isStudent: Bool { get{
        return age < 20
        }}
}

We define isStudent() methods for each person now. I checked Liam and Ella.

Initiate each person and use XCTAssert() method to check whether isStudent is True or not.

Because they are students, it returns true and show u green square signal.

As you can see, red square signal is shown when the condition of isStudent method is not true.

Conclusion

Unit test is useful to serperate and manage test code from source code. It is good to find strategy which code should be tested.

Mocking and Stubbing make it effective ,so following posting will be about these thing.

Hope this post is helpful to you.

Reference