为什么 xUnit 找不到我的 DNX 测试?

Why can't xUnit find my DNX tests?

我正在开始一个新的 ASP.net vNext / MVC6 项目,我想使用 xUnit 来测试它。

我已经关注 instructions on the xUnit web site 的 DNX 项目。

当我尝试 运行 Visual Studio 中的测试时,无论是使用内置 运行ner 还是在 ReSharper 中,我都会收到此错误消息:

如果我尝试 运行 从命令行进行测试,我会得到:

xUnit.net DNX Runner (32-bit DNX 4.5.1)
  Discovering: TA.Product.Tests
  Discovered:  TA.Product.Tests
  Starting:    TA.Product.Tests
    TA.Product.Tests.Class1.FactMethodName [FAIL]
      System.NullReferenceException : Object reference not set to an instance of an object.
      Stack Trace:
        D:\VS-Projects\TA.Product\src\TA.Product.Tests\Class1.cs(25,0): at TA.Product.Tests.Class1.FactMethodName()
  Finished:    TA.Product.Tests
=== TEST EXECUTION SUMMARY ===
   TA.Product.Tests  Total: 1, Errors: 0, Failed: 1, Skipped: 0, Time: 2.216s

这是我的 project.json 文件:

{
  "version": "1.0.0-*",
  "description": "TA.Product.Tests Class Library",
  "authors": [ "Tim" ],
  "tags": [ "" ],
  "projectUrl": "",
  "licenseUrl": "",
  "dependencies": {
    "TA.Product": "1.0.0-*",
    "xunit": "2.1.0",
    "xunit.runner.dnx": "2.1.0-rc1-build204"
  },
  "commands": {
    "test": "xunit.runner.dnx"
  },
  "frameworks": {
    "dnx451": { },
    "dnxcore50": {
      "dependencies": {
        "Microsoft.CSharp": "4.0.1-beta-23516",
        "System.Collections": "4.0.11-beta-23516",
        "System.Linq": "4.0.1-beta-23516",
        "System.Runtime": "4.0.21-beta-23516",
        "System.Threading": "4.0.11-beta-23516"
      }
    }
  }
}

我发现 ,我的 xUnit 运行ner 版本似乎与我的 DNX 版本 RC1-Update1 匹配。

我还发现 this question 说我的测试必须是 public,它们是。

有什么想法吗?

当您从命令行 运行 dnx test 并获得这些结果时:

Finished:    TA.Product.Tests
=== TEST EXECUTION SUMMARY ===
   TA.Product.Tests  Total: 1, Errors: 0, Failed: 1, Skipped: 0, Time: 2.216s

...这实际上是一个成功的测试 运行。唯一的问题是您的 1 测试失败了。

我想当它 "crashes" 时,您正在尝试 运行 使用 ReSharper xUnit 运行ner 扩展。 运行ner 当前(从 rc1 开始)not 与 DNX 项目一起工作,因为 R# 在不存在的位置查找已编译的测试程序集。尝试禁用 resharper xUnit 运行ner 扩展并从默认 Visual Studio Test Explorer 运行ning 测试。它应该有效。

然后,当您需要 运行 测试时,不要使用任何 R# 按钮或快捷方式。请改用内置的 VS 测试按钮和命令(或者只是从命令行 运行 dnx test,这应该更快)。弄清楚后,您可以为其他非 DNX 项目重新启用 R# xunit 运行ner。