Testing your Kitura apps

This guide will walk you through setting up tests in accordance of the XCTest framework, which is bundled with the Swift binary.

Setting up the Tests directory

In the root level of your project, create a folder called Tests.

Inside the Tests directory you’ll need to create a folder for holding your test suite module. It is recommended that it is the same name as your App module.

Creating Test Cases

Create a new Swift file in the test suite folder.

When setting up this file you’ll want to add @testable import <module-name> for each of the modules you want to test. (Note the @testable keyword allows you to call internal methods in your tests)

Create your testing class as a subclass of XCTestCase

Create each test case as a different function within the testing class. Note that each function must begin with the string “test”. You can use Apple’s guide for tips on how to make good test cases.

For Linux compatibility, you will need to add an additional class variable to your test class named allTests which has the signature:

static var allTests : [(String, (MyModuleTests) -> () throws -> Void)]

This should hold an array of tuples matching a string name to the function name for each test case.

Creating Test Main (Linux only)

Inside your Tests directory, create a new file called LinuxMain.swift.

In this file import all your test modules. The names of the test modules are <folder-name>test.

Import XCTest as well

Invoke XCTMain with an array as follows:


Run Tests

In your projects root directory, run the command swift test after having built them using swift build.

Example Test File

@testable import MyModule

class MyModuleTests: XCTestCase {
    static var allTests : [(String, (MyModuleTests) -> () throws -> Void)] {
        return [
            ("testAsserts", testAsserts)

    func testAsserts() {
        XCTAssertEqual(1, 1, "Message shown when assert fails")
        XCTAssertNil(foo, "Message shown when assert fails")
        XCTFail("Message always shows since this always fails")
        // Other Asserts can be used as well