IOS Test Frameworks and Unit Testing Brief Explanation 

# IOS Test Frameworks
Maybe you already developed an application that is working properly, but you might not have any test set up for it and of course you may want to test any changes when you want to extend the application functionality.

In IOS there are lots of Test Frameworks, created for developers to test their applications to make sure that all the functionalities are working fine before shipping them to the users. This article will list some of the popular Test Frameworks for IOS application.

1. <b>Appium</b> : is an open-source testing tool. It provides automations of application testing process on <i>Android</i> and <i>IOS</i>. It allows the user to check each individual parts of the program's source code, and systematically done during the process of programming.
2. <b>Calabash</b> : is an IOS automation testing tool. The testing process of this framework is done based on <a href="https://cucumber.io/">Cucumber</a> (an extensive automation testing tool)
3. <b>XCTest</b> : is the IOS functional testing framework. This framework can be used for testing at any state of the development process.
4. <b>EarlGrey</b> : is the best UI testing framework. This open-source framework is easy to integrate with IOS.
5. <b>Jest</b> : is an automated testing framework. Using this framework, we can ensure that the user interface cannot be changed unexpectedly.

As mentions earlier there are lots of Test Framework available for IOS, but in this article I will walk you through the built-in IOS Unit testing itself.

# IOS Unit Testing
To demonstrate of how we can implement Unit Test in IOS I create a sample project <a href="https://github.com/phanithken/ArithmeticUnitTest">ArithmeticUnitTest</a> in Github. It's basically an app to print the value of arithmetic operation to the console by giving sample values. Now clone it into your local computer and open the project in Xcode.
### Create a Unit Test Target in Xcode
Now, open test navigator and click on the + sign at the lower left corner and then select <b>New Unit Test Target</b>
![TestNavigator1.png](https://qiita-image-store.s3.amazonaws.com/0/211892/f8dbdc36-932f-96f2-6d8a-faae09c6385c.png)
To run this test class you can just simple go to <b>Product -> Test</b> or <b>Command + U</b>
#### Using XCTAssert to test the Model
On top of your test class add line below the import statement:

```swift
@testable import UnitTestSample
```
It will give you the unit test access to classes and methods in UnitTestSample
Now, let's create a property of <b>Arithmetic</b> class in the test class

```swift
var arithmeticUnderTest: Arithmetic!
```
then initialize in <b>setup()</b>, It will create SUT (System Under Test), so that all the tests in this test class can access to the SUT's object property and methods.

```swift
override func setUp() {
    super.setUp()
    self.arithmeticUnderTest = Arithmetic(a: 10, b: 5)
}
```
and then you have to release the SUT object in <b>tearDown()</b>

```swift
override func tearDown() {
   self.arithmeticUnderTest = nil
   super.tearDown()
}
```
Now, we are ready to write our Arithmetic Unit test. Let's go and delete <b>testExample()</b> and add these following methods:

```swift
func testAddition() {
    XCTAssertEqual(self.arithmeticUnderTest.addition(), 15, "Addtion Process is wrong!")
}
    
func testSubstraction() {
    XCTAssertEqual(self.arithmeticUnderTest.substraction(), 5, "Substraction Process is wrong!")
}
    
func testMultiplication() {
    XCTAssertEqual(self.arithmeticUnderTest.multiplication(), 50, "Multiplication Process is wrong!")
}
    
func testDivision() {
    XCTAssertEqual(self.arithmeticUnderTest.division(), 2, "Division Process is wrong!")
}
```
A test method's name always begin with <i>test</i>.
Right in the above given methods, we give a sample value at the initialization part in <b>setup()</b> int a = 10 and int b = 5. Now let's run the test and see the result. If the result is passed, the diamond button on the right hand of each test functions will turn green.
#### Debugging a Test
In this testing method, you can also add <b>Test Failure Breakpoint</b>. By creating this the test program will stop running when it encounter any failure. You can create it by navigating to the <b>Breakpoint Navigator</b> and click on the + sign at the lower left corner and then select <b>Test Failure Breakpoint</b>
![AddTestFailureBreakpoint.png](https://qiita-image-store.s3.amazonaws.com/0/211892/3092538c-d3f2-1318-7914-a97091c3a508.png)
Now go ahead and change the result of arithmetic operation to see how it look.

```swift
func addition() -> Int {
    return self.a - self.b
}
```
Now that you have change the result of addition, then it will give you the error when you attempt to run test.
<img width="432" alt="screenshot.png" src="https://qiita-image-store.s3.amazonaws.com/0/211892/65903002-ece5-1385-6b10-08c4c5facf9c.png">

Cool! That's all for the demonstration now, but there are lots more we can do with this IOS Unit Test like Asynchronous test operation..etc stuff like that.