如何在失败时让MStest返回非零退出代码?

时间:2016-07-26 22:47:53

标签: mstest .net-core exit-code

我正在尝试使用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"功能"。也许我应该考虑一些替代测试跑步者。

1 个答案:

答案 0 :(得分:2)

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