Unit testing in AppCode
AppCode: 2019.3 or later
Xcode: 11 or later
Initial project: iOSConferences
Final project: iOSConferences
In this tutorial, we will write simple unit tests in AppCode using different testing frameworks — XCTest and Quick/Nimble. You will learn how to create test classes and targets, run and debug tests, view the test coverage information, and more.
Before you start
For this tutorial, we will use the iOSConferences application that we developed in the Use CocoaPods in your project tutorial. This application displays the up-to-date list of the upcoming conferences from the cocoaconferences.com website.
Download the iOSConferences project.
Open the application in AppCode using the iOSConferences.xcworkspace file so that the pods already added to the project can be recognized by the IDE.
If CocoaPods gem is not installed yet, install it:
Click Tools | CocoaPods | Select Ruby SDK from the main menu.
In the Preferences dialog that opens, click Add Ruby SDK and specify the path to the SDK (by default,
/usr )./bin /ruby Click Install CocoaPods.
In the run/debug configuration selector, select a device or simulator to run the application on and press ⇧ F10 or click
:
The launched application should display the list of conferences:
tip
If you encounter problems with running the application, make sure that you are using AppCode 2019.3 or later (with SwiftUI supported) and CocoaPods is successfully installed on your side.
Step 1. Add a test target
When creating a new project, you can select the Include Unit Tests checkbox to have a test target and an XCTest class added. However, you can add it anytime while working with an existing project.
Press ⌃ ⌥ ⇧ S to open the project settings.
Click
, select iOS | Test | Unit Testing Bundle from the dialog that opens, and click Next:
On the next page, leave the default values in all the fields including the automatically generated target name iOSConferencesTests in the Product Name field and click Finish:
A new test target will be added to the project.

This target contains a default XCTest class with stub code for several test methods:

Step 2. Create XCTest tests
Let's create some tests to check if the conference date is displayed correctly on the details screen.
Rename the
iOSConferencesTests
class toDateTests
using the Rename refactoring ⇧ F6:GifImport the Yams library that is used for decoding the YAML file:
import Yams
Make the application code available for tests by adding the following line right after the import statements:
@testable import iOSConferences
Delete all existing stub code inside the
DateTests
class.In the
DateTests
class, add thedecoder
instance variable:let decoder = YAMLDecoder()
Add the following test methods:
func testSameStartEndDatesShownCorrectly() { let yaml = try! decoder.decode([Conference].self, from: """ - name: mDevCamp link: https://mdevcamp.eu/ start: 2019-05-30 end: 2019-05-30 location: 🇨🇿 Prague, Czech Republic """ ) let conference: Conference = yaml[0] let textDate = conference.textDates() XCTAssertEqual(textDate, "May 30, 2019") } func testDateWithoutEndShownCorrectly() { let yaml = try! decoder.decode([Conference].self, from: """ - name: mDevCamp link: https://mdevcamp.eu/ start: 2019-05-30 location: 🇨🇿 Prague, Czech Republic """ ) let conference: Conference = yaml[0] let textDate = conference.textDates() XCTAssertEqual(textDate, "May 30, 2019") } func testEndEarlierThanStartReplaced() { let yaml = try! decoder.decode([Conference].self, from: """ - name: mDevCamp link: https://mdevcamp.eu/ start: 2019-05-30 end: 2019-05-29 location: 🇨🇿 Prague, Czech Republic """ ) let conference: Conference = yaml[0] let textDate = conference.textDates() XCTAssertEqual(textDate, "May 29, 2019 - May 30, 2019") }
These methods test if the application shows the conference dates correctly in some specific cases:
testSameStartEndDatesShownCorrectly()
: if the values in thestart
andend
fields are the same, the application should display just one date instead of an interval (May 30, 2019
instead ofMay 30, 2019 - May 30, 2019
).testDateWithoutEndShownCorrectly()
: if there is noend
value specified for the conference, the application should display just the start date (May 30, 2019
).testEndEarlierThanStartReplaced()
: if the start date is later than the end one, they should be replaced (May 30, 2019 - May 31, 2019
instead ofMay 31, 2019 - May 30, 2019
).
We will run and debug the created tests later. Now, let's see how to create tests with the Quick and Nimble frameworks.
Step 3. Create Quick/Nimble tests
Let's create some tests that will check if the application loads and parses the conferences data properly.
Step 4. Run and debug tests
In AppCode, you can quickly run and debug all methods in a test class as well as single methods (for XCTest) using the gutter icons in the editor /
or the ⌃ ⇧ F10/⌃ ⇧ D shortcuts. In this case, a temporary run/debug configuration is created which you can save and edit when needed.
For the XCTest framework, you can also run an arbitrary set of methods of one class by selecting and running ⌥ ⇧ R them from the Run tool window or by creating a separate run/debug configuration where these methods are listed. See more in Create run/debug configurations for tests.
In this tutorial, we will create a run/debug configuration for running all the test classes available in the project, debug and fix failed tests, and check how much of the application code our tests cover.