đ Unit Testing in Swift: The Fundamentals

In this first part of the Unit Testing in Swift series, we will cover how to add a unit test target to your project and write your first unit test using the XCTest framework provided by Apple. If you, for whatever reason, missed the introduction to this series, you can read it here.
This part of the series is for you if you want to learn how to start unit testing your own Xcode project or simply just get a better understanding of the fundamentals of unit testing with XCTest in Swift.
Adding a unit test target
If you are starting completely from scratch, you will need to add a unit test target to your Xcode project. Donât worry, this only takes 30 seconds. Click on the Test Navigator tab in the Xcode Navigator (the left-most window). This tab can also be reached by pressing â + 6. In the bottom left corner you can add the test target by pressing the â+â button. Follow the instructions on the screen and voila: You are now ready to write your first unit test.


XCTestCase
Now that we are ready to write unit tests you will find that Xcode has added a unit test class for you. This class inherits the XCTestCase class, which is the base for all unit tests that you will be adding. A test case usually contains setup and teardown functions. These functions are called before and after each of the test functions, respectively. This allows us to set up prerequisites before the tests are being run and to clean up any mess weâve made after the completion of each unit test.

Prefixing a function in an XCTestCase class with âtestâ, tells Xcode that the given function is a unit test. For each unit test function you will see an empty diamond shape in the gutter on the line of the function declaration. Clicking this diamond will run the specific unit test. The whole suite of unit tests can be run in various ways, but my preference is the shortcut â + U.
In terms of naming conventions for the test classes and functions, I personally prefer following Appleâs suggestions:
To help clarify the organization of your tests, give each test case a name that summarizes the tests within it, such as
TableValidationTests,NetworkReachabilityTests, orJSONParsingTests.
To help identify failing tests, give each test method a name that makes it clear what is tested by that method, such as
testEmptyTableRowAndColumnCount(),testUnreachableURLAccessThrowsAnError(), ortestUserJSONFeedParsing().
If you are brand new to unit testing these naming examples may not make a lot of sense to you, but by the end of this series, they will.
Asserting
A unit test function is worth nothing without assertions. For that, we use the XCTAssert functions. There are many different assertion functions and I will not be covering all of them, however some of the most used are: XCTAssertTrue, XCTAssertFalse, XCTAssertNotEqual, XCTAssertEqual, XCTAssertNil and XCTAssertNotNil. I personally prefer using these Xcode providedXCTAssert functions, however there are multiple third-party alternatives available â for instance many people enjoy using Nimble.
Example
Letâs be honest⌠We wonât learn a lot from just reading about it, so it is time to add some code. For the sake of this article, we will be using a very simple example of schools with students and no complex logic.




