Unit Tests. The beginning.

Ilia Stukalov
Sep 5, 2018 · 3 min read

You are a great coder. At least I hope so.
Even if you write clean code following SOLID, DRY and KISS principals, your apps can have errors. Obviously because of the algorithm. You can create great logic for your new data transfer algorithm but it would not work if you make some mistakes when you implement it in code.
Unit codes let you test the logic of your code automatically. You need to write a test and it will run each time you make any changes to your code.

Let’s code!
Oh, stop. Download starting project which I made for you.
https://yadi.sk/d/2qbUCjKJNRAccQ

There is only one class which contains logic, and we will test it.

import Foundationclass Math: NSObject {static func add(value1:Int, value2: Int) -> Int {
return value1 + value2
}

static func subtract(value1:Int, value2: Int) -> Int {
return value1 — value2
}

static func multiply(value1:Int, value2: Int) -> Int {
return value1 * value2
}

static func Divide(value1:Int, value2: Int) -> Int {
return value1 / value2
}

}

Open test navigator and click on +

Select “New Unit test target”.
You will see the freshly created class which inherits from XCTestCase.

import XCTestclass UnitTestsStarterTests: XCTestCase {

override func setUp() {
super.setUp()

}

override func tearDown() {

super.tearDown()
}

func testExample() {

}

}

Let’s pass over this code:

setUp() — should contain code which must be executed before all test in this class performs. You should put some special initializers or make connections to any data sources.

tearDown() — should contain code which must be executed after all test in this class performs. Close connections and do some other things which should be finished before the end.

testExample() — Here is our first test
put in it

 XCTAssertEqual(Math.add(value1: 2, value2: 2), 4)

Now press diamond icon near function name to run this test. It will become green. This means that the test passed successfully.

XCTAssertEqual is a function which (obviously) checks if two passed values are equal. The easiest example is to check if your function calculates everything correctly. For example, you need to check if the restaurant near your user, so you need to calculate the distance between users coordinate and location of the restaurant.

Let’s make a new function and name it testMinus. We should test that subtract function works properly.
Go forward and write code by yourself. Then compare it with my version.

XCTAssertEqual(Math.subtract(value1: 2, value2: 2), 0)

I hope you understand XCTAssertEqual.

Now let’s move forward and use XCTAssertLessThan
This function is used to check if the first value is less then second.
Create a new function to test it.

func testMultiply() {
XCTAssertLessThan(Math.multiply(value1: 2, value2: 2), 5)
}

Here we test that 2*2 is less than 5. I hope this is correct.

There is one more function we should use today.
XCTAssertGreaterThan
This function is used to test that the first value is greater than second.

Let’s test it

func testDivide() {
XCTAssertGreaterThan(Math.divide(value1: 2, value2: 2), 0)
}

Hope it works.

Please test your own values and custom test combinations.

That is all for today.

Follow me and soon I will post the second part of this tutorial.

If you have any questions, you can leave a comment or ask me on my email: istukalov96@yandex.ru.

Hope to see your apps on the AppStore!

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade