gpt4 book ai didi

c# - 如果使用 vstest.console.exe 的一项测试失败,如何停止测试?

转载 作者:行者123 更新时间:2023-11-30 16:03:23 25 4
gpt4 key购买 nike

我有一个批处理文件,其中包含以下面类似形式定义的多个测试。

vstest.console.exe Test.dll /Settings:"test.runsettings" /Tests:"t1,t2,t3,t4,t5"

测试按顺序从 t1 到 t5 运行。但是,如果任何一项测试失败,我想停止 vstest。这可能使用 vstest.console.exe 吗?

顺便说一下,我的 test.runsettings 的内容是

<?xml version="1.0" encoding="utf-8"?>
<RunSettings>
<MSTest>
<ForcedLegacyMode>true</ForcedLegacyMode>
<KeepExecutorAliveAfterLegacyRun>true</KeepExecutorAliveAfterLegacyRun>
</MSTest>
</RunSettings>

我检查了 Documentation for runsettings , 这种情况似乎没有标志/属性。

最佳答案

如果它适合您,那么您可以为具有清理、初始化方法和 TestContext 属性的测试引入基类。

在清理方法中,您将检查测试是否失败,并通过在 TestInitialize 中触发 Assert.Fail,您不允许任何其他测试在此之后通过。

[TestClass]
public class BaseTest
{
private static bool _failAllTests;
public TestContext TestContext { get; set; }

[TestInitialize]
public void InitializeMethod()
{
if (_failAllTests)
{
Assert.Fail("Fail all tests");
}
}

[TestCleanup]
public void CleanUpMethod()
{
if (TestContext.CurrentTestOutcome == UnitTestOutcome.Failed)
{
_failAllTests = true;
}
}
}
[TestClass]
public class UnitTest1 : BaseTest
{
[TestMethod]
public void TestMethod1()
{
Assert.Fail("TestMethod1 failed!");
}
[TestMethod]
public void TestMethod2()
{
Assert.IsTrue(true, "TestMethod2 passed!");
}
}

关于c# - 如果使用 vstest.console.exe 的一项测试失败,如何停止测试?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36449901/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com