【问题标题】:How can I get MStest to return a non-zero exit code when failing?失败时如何让 MStest 返回非零退出代码?
【发布时间】:2016-11-30 18:19:12
【问题描述】:

我正在尝试使用 MSTest 测试一些 .NET Core 代码,但我发现即使测试失败,测试套件也会返回零退出代码。我可以使用来自the blog post announcing the technology 的代码示例重现该问题。重现问题的代码是on github,下面转载重要部分。

project.json

{
  "version": "1.0.0-*",

  "testRunner": "mstest",

  "dependencies": {
    "dotnet-test-mstest": "1.0.1-preview",
    "MSTest.TestFramework": "1.0.0-preview"
  },

  "frameworks": {
    "netcoreapp1.0": {
      "imports": [
        "dnxcore50",
        "portable-net45+win8"
      ],

      "dependencies": {
        "Microsoft.NETCore.App": {
          "version": "1.0.0",
          "type": "platform"
        }
      }
    }
  }
}

ExitCodeTests.cs

using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace SampleNetCoreUnitTests
{
  [TestClass]
  public class TestClass
  {
    [TestMethod]
    public void TestMethodPassing()
    {
      Assert.IsTrue(true);
    }

    [TestMethod]
    public void TestMethodFailing()
    {
      Assert.IsTrue(false);
    }
  }
}

当我使用dotnet test && echo THIS SHOULD NOT BE ECHOED 运行测试时,我得到以下输出:

Discovering Tests ...
Executing Tests ...
Passed   TestMethodPassing
Failed   TestMethodFailing
Error Message:
   Assert.IsTrue failed.
Stack Trace:
   at SampleNetCoreUnitTests.TestClass.TestMethodFailing() in /app/ExitCodeTests.cs:line 17
============ Test Run Summary ============
Total tests: 2. Passed: 1. Failed: 1. Skipped: 0
Test Run Failed.
SUMMARY: Total: 1 targets, Passed: 1, Failed: 0.
THIS SHOULD NOT BE ECHOED

即使我的测试失败,测试套件返回的退出代码为零,因此正在执行 echo 命令。

没有切换到 xUnit 等替代方案,如何让我的测试套件在失败时返回非零退出代码?

编辑: The author of this stackoverflow answer 似乎认为 MSTest 即使在失败时也应该返回零退出代码。也许这是 MSTest 的“功能”。也许我应该考虑一些替代测试运行器。

【问题讨论】:

  • 只是为了结束循环,我会在这里评论并说我通过切换到 xUnit 解决了我的问题。
  • 我希望 MS 中的一些理智的人能在接下来的半年左右恢复这一点)

标签: mstest .net-core exit-code


【解决方案1】:

此问题现已修复,并在此处更新 NuGet:https://www.nuget.org/packages/dotnet-test-mstest/1.1.1-preview

【讨论】:

    猜你喜欢
    • 2015-09-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-28
    • 2019-12-21
    • 1970-01-01
    • 2016-07-03
    • 1970-01-01
    相关资源
    最近更新 更多