1
votes

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.

  1. Create a new project.
$ vapor new Foo
Cloning Template [Done]
$ cd Foo
$ mkdir -p Tests/ModelTests
  1. 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
  1. Build the project.
$ vapor build
No Packages folder, fetch may take a while...
Fetching Dependencies [Done]
Building Project [Done]
  1. Run the tests.
$ vapor test
Testing [Failed]
Log:
swift-test: error: no tests found to execute, create a module in your `Tests' directory
  1. Oops, seems like we need to remove "Tests" from the exclude: section of Package.swift.
$ vi Package.swift
   ... remove Tests from exclude: ....
  1. 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
  1. 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.

1
I believe naming of test modules is enforced by SwiftPM, try naming your test module AppTests and see if that resolves some of the issues.Logan
Oh my god. Where do I send your bottle of the world's finest whiskey?Mark Smith

1 Answers

2
votes

Put in comments, but just so people can see an answer, I'll mark here too:

SwiftPM enforces naming conventions on tests, so the proper naming would be <#YourTarget#>Tests.

In this case, rename your test module AppTests and that should do the trick!