【发布时间】: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