Here's where we can finally practice - we'll get to create our first test!
No more waiting, here's our grand plan:
We'll first create a target in our project (this is where we'll write all our tests).
Then we'll add a test file to the new target.
And, finally, we'll write our tests in this file!
Let's go! 💪
Create a target
What's a target?
Xcode works with projects. When you create a new Xcode project by default, it creates a single target: your application. A project can actually contain multiple targets.
For example, you can have multiple versions of the same application: a pro version and a lite version. These two applications are close enough to be part of the same project, but they are not identical, so they'll form different targets.
The tests are not part of the application. They behave like a separate component. After all, they need not be included in the final product. They are an assisting element used during the development phase. Therefore, tests are going to be a separate target.
To create a new target, do the following:
Select the project in the browser and click the +
button ate the bottom left corner of the left side of the Editor:
In the pop-up that results, make sure you are on the iOS tab. There are quite a number of options here, so to speed up the process, type "test" in the search bar. Two tests will show up; choose iOS Unit Testing Bundle and hit Next:
Then Xcode offers a few options to configure the new target. They are the same as when creating an Xcode project, with the exception of the last two, which allow us to specify the project in which to create the target and the target that will be tested (that target being our application). Click Finish to create the target:
And here our target is created! It now appears on the list of project's targets and a new folder has been created in the browser:
Add a test file
As you may have noticed, the target already contains a test file named TicTacToeTests.swift
. It's a generic file and we'll need to be more specific. So, you may delete this one as we'll have no need for it.
Instead, we are going to create a new one. To do so, select the TicTacToeTests folder, then press cmd + n (or go to the main menu, File ➡️ New ➡️ File ..., or right click on the folder and select New File...'from the menu). Make sure the iOS tab is selected, choose Unit Test Case Class. Then click Next:
Then name your GameTestCase file, because we will start by testing the Game class. And click Next:
Finally, make sure that the selected target is that of the tests and click Create:
Your first test file has been created! You can spot it in the Navigator:
Presentation of XCTestCase
Let's take a look at the file we've just created:
Let's review its default contents:
We start by importing
XCTest
which is the Xcode test framework.Then we create a
GameTestCase
class, which is a subclass ofXCTestCase
.And we won't need the rest of default methods - so, go ahead and delete the contents of the subclass.
As the name suggests, XCTestCase
is a class that allows you to do tests. Whenever you want to create a test, you'll need to create a subclass of XCTestCase. We'll need to create a separate file for each class we intend to test. Each new class will subclass the XCTestCase class and will include a few tests necessary to test members of corresponding class from the main application.
Creating your first test
After we've deleted the default content of the test subclass, the code should look look like this:
import XCTest
class GameTestCase: XCTestCase {
}
Add the test
And now, let's add our very first test! To do that, we need to add test methods to the subclass using the test
prefix in the name:
func testMyMethod() {} // This is a test
func myMethod() {} // This is NOT a test
Let's start by adding a test example to out test class:
class GameTestCase: XCTestCase {
func testExemple() {
}
}
XCTAssert
Now we'll complete this test!
Remember, the test's evaluation is a boolean. If the resulting boolean is true, the test passed; otherwise, it failed.
The function that evaluates the boolean is called XCTAssert
and takes a single parameter - a boolean!
The code looks like this:
func testExample () {
XCTAssert (true)
}
The asserting function will evaluate whether the boolean is true. And so the test in the example above will pass.
Start the test
To execute the test, simply click on the small diamond-shaped icon to the left of the test method:
After running the test, since we are explicitly passing it the true value, the test will pass:
Now, we can test the false value instead. After running the test, we can observe the opposite result - the test failed:
Go ahead and play with the values a little, passing different examples of parameters to this function:
XCTAssert(2 * 2 == 4)
XCTAssert(true || false)
XCTAssert(true && false)
XCTAssert(365 % 7 == 0)
XCTAssert([1, 2, 3, 4].count == [5, 6, 7, 8].count)
There are different ways to run the tests:
To run all the tests for the project, you can use the cmd + u shortcut.
To run all the tests of a class, you can click on the diamond next to theclass declaration.
To run a single test, click on the diamond next to the test.
A bit of work left. Go ahead and create a new file for a test subclass for the Play class, PlayTestCase.swift
, and include the initial content. Good luck!
Let's Recap!
Tests for an application require the creation of a separate target in a project. The application itself forms a target and the corresponding tests, another. Both targets are part of the same project.
The test target consists of test files that implement a subclass of the
XCTestCase
class. Each testable class should have a separate test subclass.A single test is implemented as a method of a test subclass and starts with the prefix
test
.The
XCTAssert
function evaluates a Boolean: if it's true, the test is considered successful; otherwise, failed.To start the tests, you can click on the diamond shaped icon along the test code or use the cmd + u shortcut to run all the tests of the project.