Unit Test - Swift

6726 단어 Swifttesting

Introduction


This article we are going to cover the Unit Test which I think all the developer should do or know about this.First of all , we are going to discuss about the basic concept and definition of unit test and how to use it along with the example at the end.Ok So now, let's get started.

1. What Is Unit Test?


Unit Test is a way where we can write the code to do some testing about our application that we have been develop to checked whether are there any bug? whether does all the code is working correctly?. In short, Doing Unit test is to prove that our code Work and prevent bug.

2. How To Do Unit Test?


In swift , It provide XCTest. So we can use it to test our application code/function and there are also another third party Unit Test that we can use for example like :
Quick & Nimble.

3. Requirement


We can't do Unit Test with all the code. First of all , if you want to do Unit test, you have to make sure that your code is organize. What do I mean by organize? Well , for example, You develop an app with only 1 class , And that class contain everything , It contain the User Interface , You do the business logic there and you even store the data there. Well, if that so , You will have a hard time doing the Unit test. But don't worry, I will list some requirement below so you will be able to do Unit Test very easy.
1. Each class should focus or responsible for only 1 thing.
2. Using Dependency Injection.

Example


So First , let's us create a sample xcode project. I name my project as "UnitTestSampleProject

Now, let create a new class which contain about the Information of A student.
class Student {

    var firstname : String
    var lastname : String

    init(firstname : String , lastname: String) {
        self.firstname = firstname
        self.lastname = lastname
    }

    func validateFirstname() -> Bool {
        return self.firstname.count > 3
    }

    func validateLastname() -> Bool {
        return self.lastname.count > 3
    }
}
In above class, We see there are :
1. Two class properties which are firstname and lastname and we are using init() to initialize the its value.
2. We have two function which we use to validate student firstname and lastname.
Now let create Unit Test. You can create it by select the Testing Tab click and Plus Sign and choose New Unit Test Class.

You will see below code

Let delete everything and make our first Unit test. and let start by using @testable import UnitTestSampleApp

this @testable import allow the unit test class to be able access to our main code.
let start with the validatefirstname test. Remember that, the naming convention in Unit Test class is really important because if you are refactor or change the code in the future , the function and code here need to be change as well, so you have to put the func name as descriptive as possible and every func name must start by test.

above code is , we create a student object and call the validateFirstname method to validate the name of the student object. Since our method return bool so we have to use XCTAssertTrue to test the name. you can run the test by clicking on the tick mark at the left hand side.

Ok now let do testing with validateLastname.

You can try give the name less than 3 characters and see what will happen. The test will failed and you will get notify about its field and that's it for today.

좋은 웹페이지 즐겨찾기