【问题标题】:VS Test Explorer is not finding my Unit Tests (XUnit.Runner) ASP.NET 5VS Test Explorer 没有找到我的单元测试 (XUnit.Runner) ASP.NET 5
【发布时间】:2016-01-21 22:30:46
【问题描述】:

我在 XUnit 中使用 ASP.NET 5,而 Visual Studio 在测试资源管理器中找不到我的测试。

我已经多次重建我的项目以让它们刷新。我的测试资源管理器是空的。

有什么想法吗?

这是我的 project.json 文件:

{
  "version": "1.0.0-*",
  "description": "TestLibrary",
  "authors": [ "brivell" ],
  "tags": [ "" ],
  "projectUrl": "",
  "licenseUrl": "",

  "dependencies": {
    "AutoFixture": "3.38.1",
    "AutoFixture.AutoMoq": "3.38.1",
    "BusinessLibrary": "1.0.0-*",
    "xunit": "2.1.0",
    "xunit.runner.dnx": "2.1.0-rc1-build204"
  },

  "frameworks": {
    "dnx451": { }
  }
}

这是我的一个测试示例:

[Fact]
public void Traditional()
{
    // Arrange
    var sut = new Calculator();

    // Act
    sut.Subtract(1);

    // Assert
    Assert.True(sut.Value < 0);
}

【问题讨论】:

  • 您需要在测试上方添加 [TestMethod] 属性。 msdn.microsoft.com/en-us/library/…
  • @DTmann 该属性仅在使用 Microsoft UnitTesting 库时有效。 Xunit 使用属性[Fact]

标签: c# visual-studio unit-testing asp.net-core xunit


【解决方案1】:

您需要在测试项目中安装包 XUnit.Runner.Console 以便测试运行程序发现您的测试。

https://www.nuget.org/packages/xunit.runner.console

【讨论】:

  • xunit.github.io/docs/… 是说明使用它的文档,也是不同 Visual Studio 版本的警告。
  • 我在文档中看到了这一点,但认为只有当我想从控制台执行测试时。但显然这就是 vs 执行它们的方式。谢谢!
【解决方案2】:

通过project.json 中的以下 3 个 XUnit 特定属性,我可以使用 Visual Studio 的测试资源管理器来发现我的 XUnit 测试:

{
  "version": "1.0.0-*",
  "testRunner": "xunit",
  "dependencies": {
    ...
    "xunit": "2.1.0",
    "dotnet-test-xunit": "2.2.0-preview2-build1029",
    ...
  },
  ...
}

【讨论】:

  • 文档中缺少的关键部分是“testRunner”属性。添加它使 VS 能够找到测试。
猜你喜欢
  • 2016-04-10
  • 1970-01-01
  • 2013-02-26
  • 2012-07-31
  • 2022-10-13
  • 2012-08-17
  • 2021-07-23
  • 2011-07-28
  • 1970-01-01
相关资源
最近更新 更多