测试在 vapor 应用程序中不起作用

tests not working in vapor app

我一直无法在我的 Vapor 应用程序中进行测试。链接器似乎没有找到任何正在测试的应用 classes。为了缩小问题范围,我尝试使用默认应用程序模板创建最简单的测试。步骤如下所示。如果有人可以告诉我我做错了什么,或者他们可以重现这个问题,我将不胜感激。

  1. 创建一个新项目。
$ vapor new Foo
Cloning Template [Done]
$ cd Foo
$ mkdir -p Tests/ModelTests
  1. 在默认项目中添加引用 class 的虚拟测试。
$ cat > Tests/ModelTests/PostTests.swift
import XCTest
@testable import App

class PostTests: XCTestCase {
  func testPost() {
    print(Post.self)
    XCTAssertEqual("a", "a")
  }
}
^D
  1. 构建项目。
$ vapor build
No Packages folder, fetch may take a while...
Fetching Dependencies [Done]
Building Project [Done]
  1. 运行 测试。
$ vapor test
Testing [Failed]
Log:
swift-test: error: no tests found to execute, create a module in your `Tests' directory
  1. 糟糕,我们似乎需要从 Package.swift.
  2. 的 exclude: 部分中删除 "Tests"
$ vi Package.swift
   ... remove Tests from exclude: ....
  1. 再试一次。
$ 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. 尝试直接执行上面显示的命令行。
$ /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

这也是我在自己的项目中看到的行为:链接器未从测试中引用的应用中找到 classes。

已解决,见下方评论。

发表评论,但为了让人们看到答案,我也会在这里标记:

SwiftPM 在测试中强制执行命名约定,因此正确的命名应该是 <#YourTarget#>Tests

在这种情况下,重命名您的测试模块 AppTests 应该可以解决问题![​​=12=]