forked from Kitura/Kitura
-
Notifications
You must be signed in to change notification settings - Fork 0
Testing your Kitura Apps
Ian Partridge edited this page Sep 16, 2016
·
6 revisions
This article will walk you through setting up tests in accordance of the XCTest framework which is bundled with the Swift binary.
- 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.
- 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.
- Inside your
Tests
directory, create a new file calledLinuxMain.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:
XCTMain([
testCase(TestClass1.allTests),
testCase(TestClass2.allTests)
])
- In your projects root directory run the command
swift test
after having built them usingswift build
.
@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
}
}