Learn how to test your Swift exercises on Exercism
This guide explains how to run unit tests for the Exercism Swift exercises using either macOS or Linux. It assumes you have already successfully installed the Exercism client and fetched the first exercise. If not, do that now.
1. `swift test` runs tests
2. `swift package generate-xcodeproj` creates an Xcode project
To run the tests from the command line, first cd
to the exercise directory (for example, ~/exercism/swift/exercises/hello-world/), then execute swift test
. This will compile the files in the Sources directory and execute the tests in the Tests directory. Alternatively, open the Xcode project and press Command-U.
The rest of this page explains how Xcode is structured, and how to get set up to complete the exercise and run the tests.
To skip to step by step instructions, scroll down to the section titled "Configuring Xcode" and "Running tests with Xcode".
The following instructions are written with the expectation that some readers will be very new to test driven development, Exercism, or Xcode. Each step is described in detail.
macOS users are encouraged to use Xcode. The Xcode environment provides very detailed error messages compared to those available from the command line.
Exercism uses the Swift Package Manager to package files and tests for Swift. Packages provide a complete set of source files that can be compiled and executed from the command line (or from Xcode).
To complete an Exercism exercise, you will work primarily with two files:
Optionally, Xcode can create a third file for experimentation:
cd
to the directory for an exercise (for example, ~/exercism/swift/exercises/hello-world/), then type swift package generate-xcodeproj
and press enter. This creates an Xcode project in the current directory with the same name as the package. When you run this command, Terminal prints out a statement showing you the name of the xcode project it has created. For the HelloWorld exercise, you will see: generated: ./HelloWorld.xcodeproj
. (The generated project is ideal for unit tests. It does not include the overhead of launching the iOS simulator or a target application.)open HelloWorld.xcodeproj
. You will need to change the exercise name from "HelloWorld" to whichever exercise you are working on. You will find the relevant name of the exercise from the generated
statement on your screen in step 1.HelloWorldTests
folder is closed, click on the disclosure triangle to reveal its contents.Playgrounds can be useful for brainstorming solutions to the problem. The playground continuously evaluates code as you type, displaying variable states and results almost immediately.
If you generated an Xcode project, follow these steps:
Keep in mind that while playgrounds are useful for drafting code snippets and ideas, code must be moved to the Swift Source file for testing and submission.
Select the Tests Source file from the file inspector. You can trigger tests by clicking on one of the diamonds in the gutter of the file. The diamond next to the class definition will run all the tests, whereas the diamond next to each individual test will run only that test.
Tests can also be invoked with Command-U, from the Test Inspector (Command-5), or from the sub-menu under the play button in the top bar. Red errors alongside all the tests are normal, since you have not witten any code yet!
Test driven development is a very iterative process. The first step is to successfully run the tests and have them fail. Next try "sliming a test" by creating a method that returns a correct hard coded value for the first test. Sliming will confirm the project can compile and pass one test.
Next, replace the hard coded value with code that generates the expected return value and test again. Keep repeating this cycle until all the tests pass.
Remember that the exercise is solved with code in the application source file alone. As you enter your solution into the Swift Source file, Xcode will continuously update the display with errors that highlight code that will not compile.
Once all the tests are marked with a green icon, congratulations, you have successfully completed the exercise! Now submit it to the Exercism website for review. If you are impossibly stuck, submit the exercise before it is complete to view how other users solved the exercise.
The Hello-World exercise is a very simple coding problem, but the complexity of Xcode can make even simple exercises complex. We can ignore most of this complexity, because we only need to edit one file. You can always regenerate the Xcode project if something goes wrong.
To review:
To submit:
~/exercism/swift/hello-world/HelloWorld/HelloWorld/
exercism submit HelloWorld.swift
(Alternatively, you can submit by using the file name with the full path from any directory. A second alternative is to locate the file you need to submit in the Finder. Open the Terminal, type exercism submit followed by a space, then drag the file from the Finder into the Terminal and press return.)To participate in code reviews:
XCTestCase is the XCTest subclass that contains the test's methods and XCTAssert lines are the expected results. The results are different types depending on how the test is designed.
There is a lot more to learn more about Test Driven Development, XCTAssert Methods and XCTestCase. Unfortunately up-to-date Swift documentation on these topics is short-lived. Although out of date as of when this tutorial was last updated, this list of sources contains useful information:
XCTestCase / XCTestExpectation / measureBlock()
A list of Assertions supported in XCTest.