I've been unable to get tests working in my Vapor app. It seems like the linker just doesn't find any of the app classes being tested. To narrow the problem down, I've tried creating the simplest test possible using the default app template. The steps are shown below. If anyone can either tell me what I'm doing wrong, or that they can replicate the issue, I'd be very grateful.
- Create a new project.
$ vapor new Foo Cloning Template [Done] $ cd Foo $ mkdir -p Tests/ModelTests
- Add a dummy test that references a class in the default project.
$ cat > Tests/ModelTests/PostTests.swift import XCTest @testable import App class PostTests: XCTestCase { func testPost() { print(Post.self) XCTAssertEqual("a", "a") } } ^D
- Build the project.
$ vapor build No Packages folder, fetch may take a while... Fetching Dependencies [Done] Building Project [Done]
- Run the tests.
$ vapor test Testing [Failed] Log: swift-test: error: no tests found to execute, create a module in your `Tests' directory
- Oops, seems like we need to remove "Tests" from the exclude: section of Package.swift.
$ vi Package.swift ... remove Tests from exclude: ....
- Try again.
$ vapor test Testing [Failed] Log: <unknown>:0: error: build had 1 command failures swift-test: error: exit(1): /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift-build-tool -f /Users/mark/tmp/Foo/.build/debug.yaml test
- Try directly executing the command line shown above.
$ /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift-build-tool -f /Users/mark/tmp/Foo/.build/debug.yaml test Linking ./.build/debug/FooPackageTests.xctest/Contents/MacOS/FooPackageTests Undefined symbols for architecture x86_64: "__TMaC3App4Post", referenced from: __TFC10ModelTests9PostTests8testPostfT_T_ in PostTests.swift.o __TMaMC3App4Post in PostTests.swift.o ld: symbol(s) not found for architecture x86_64 <unknown>:0: error: link command failed with exit code 1 (use -v to see invocation) <unknown>:0: error: build had 1 command failures
This is the behavior I see in my own project as well: the linker doesn't find classes from the app referenced in the test.
Solved, see comment below.
AppTests
and see if that resolves some of the issues. – Logan