Unit Test - Swift

#<b>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. 

##<b>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.

##<b>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 :
<b> Quick </b> & <b>Nimble</b>.

##<b>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.<b> Each class should focus or responsible for only 1 thing.</b>
2.<b> Using Dependency Injection.


##<b> Example </b>
So First , let's us create a sample xcode project. I name my project as "UnitTestSampleProject
<img width="270" alt="Screen Shot 2018-05-29 at 6.09.46 PM.png" src="https://qiita-image-store.s3.amazonaws.com/0/211896/7ac91ab7-2576-a3af-5122-1e4e3a4ef6a4.png">

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

```swift
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 <b>firstname and lastname</b> 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.

<img width="270" alt="Screen Shot 2018-05-29 at 6.35.46 PM.png" src="https://qiita-image-store.s3.amazonaws.com/0/211896/09d3d00c-16f9-5a04-2b9b-1b7f03ec5df7.png">

You will see below code
<img width="864" alt="Screen Shot 2018-05-29 at 6.39.37 PM.png" src="https://qiita-image-store.s3.amazonaws.com/0/211896/a5681d0b-0f96-28ab-5835-d70719770942.png">

Let delete everything and make our first Unit test. and let start by using <b> @testable import UnitTestSampleApp
<img width="324" alt="Screen Shot 2018-05-29 at 6.42.34 PM.png" src="https://qiita-image-store.s3.amazonaws.com/0/211896/ae7200a9-efa1-6ca5-89b4-72d980178c7a.png">

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. 

<img width="544" alt="Screen Shot 2018-05-29 at 6.44.33 PM.png" src="https://qiita-image-store.s3.amazonaws.com/0/211896/6f1616bf-e6d7-c391-679e-c9ed6b1d5984.png">

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.

 <img width="571" alt="Screen Shot 2018-05-29 at 6.49.04 PM.png" src="https://qiita-image-store.s3.amazonaws.com/0/211896/e7c22d59-86e3-1281-4c54-17f2e6b2ed26.png">

Ok now let do testing with validateLastname.
<img width="620" alt="Screen Shot 2018-05-29 at 6.50.12 PM.png" src="https://qiita-image-store.s3.amazonaws.com/0/211896/3cf8efc0-03ef-8b1c-e65a-3d2538027296.png">

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.