【问题标题】:How to stop test if one test fails using vstest.console.exe?如果使用 vstest.console.exe 一项测试失败,如何停止测试?
【发布时间】:2017-02-14 13:10:09
【问题描述】:

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

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,似乎没有这个案例的标志/属性。

【问题讨论】:

  • 我在 msdn 上为 TFS 构建服务器询问过这个问题。不去。不知道 vstest,虽然。
  • 你让我想起了 MSDN。我在msdn上发布了同样的问题,希望他们能得到答案,然后我可以在这里发布官方对此事的看法。我认为实现这一点的标志应该可用。但它不在文档中。
  • 不敢相信这是不可能的

标签: c# vstest


【解决方案1】:

如果要运行的测试数量很少,例如在您的示例中,您可以将其拆分为多个 vstest.console.exe 运行,并在批处理中检查 ERRORLEVEL。如果ERRORLEVEL不为0,则表示测试失败,可以退出批处理。

vstest.console.exe Test.dll /Settings:"test.runsettings" /Tests:"t1"
IF ERRORLEVEL 1 GOTO exit
vstest.console.exe Test.dll /Settings:"test.runsettings" /Tests:"t2"
IF ERRORLEVEL 1 GOTO exit
...

:exit

【讨论】:

  • 这是一个很好的建议!如果它有效,我会尝试@lukbl 建议,然后我会尝试这个。我计划同时实施两者,看看哪个更快。在每个 DLL 或测试套件的基础上实现 ErrorLevel 也是一个好主意。
【解决方案2】:

如果您可以选择,那么您可以为测试引入基类,包括清理、初始化方法和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!");
    }
}

【讨论】:

    【解决方案3】:

    除了 lukbl 的回答,您可以在程序集范围内执行相同的操作,因此如果您有多个测试类,您将在 vstest.console.exe 的运行时对测试进行全局管理(如果您例如多次调用它)。

    应根据您使用 vstest.console(或 mstest)的方式加以注意。如果您在多个测试代理之间进行负载平衡,每个测试代理将运行它们自己的 vstest.console.exe,因此会有它们自己的程序集级值,因此会话管理将受到运行的测试组的限制同一个代理。假设这种方法可以让您管理使用命令运行的整个测试集: vstest.console.exe /filter:tests.dll

    这意味着无论 session_failed 变量的范围(类范围或程序集范围)如何,如果您最终使用不同的 vstest.console.exe 调用从同一类运行不同的测试,您将丢失变量值,或者控件。

    话虽如此,多类测试场景的简单方法:

    [TestClass]
    public static class TestSettings
    {
        public static bool SessionTestsFailed = false;
    
        [AssemblyInitialize]
        public static void runsBeforeAnyTest(TestContext t)
        {
            TestSettings.SessionTestsFailed = false;
        }
    }
    
    [TestClass]
    public class Tests1
    {
        public TestContext TestContext { get; set; }
    
        [TestInitialize()]
        public void MyTestInitialize()
        {
            if (TestSettings.SessionTestsFailed)
                Assert.Fail("Session failed, test aborted");
        }
    
        [TestCleanup]
        public void MyTestFinalize()
        {
            if (TestContext.CurrentTestOutcome != UnitTestOutcome.Passed)
                TestSettings.SessionTestsFailed = true;
        }
    
        [TestMethod]
        public void test11()
        {
            Console.WriteLine("test11 ran");
            Assert.Fail("fail the test");
        }
    
        [TestMethod]
        public void test12()
        {
            Console.WriteLine("test12 ran");
            Assert.Fail("fail the test");
        }
    }
    
    [TestClass]
    public class Tests2
    {
        public TestContext TestContext { get; set; }
    
        [TestInitialize()]
        public void MyTestInitialize()
        {
            if (TestSettings.SessionTestsFailed)
                Assert.Fail("Session failed, test aborted");
        }
    
        [TestCleanup]
        public void MyTestFinalize()
        {
            if (TestContext.CurrentTestOutcome != UnitTestOutcome.Passed)
                TestSettings.SessionTestsFailed = true;
        }
    
        [TestMethod]
        public void test21()
        {
            Console.WriteLine("test21 ran");
            Assert.Fail("fail the test");
        }
    
        [TestMethod]
        public void test22()
        {
            Console.WriteLine("test22 ran");
            Assert.Fail("fail the test");
        }
    

    这是一次更新所有测试初始化​​方法的简单方法,如果它们的签名相同,则使用正则表达式匹配,Visual Studio 全部替换: 找到:

    (\s*)public void MyTestInitialize\(\)(\s*)(\r*\n)(\s*){(\r*\n)
    

    替换:

    $1public void MyTestInitialize()$3$4{$1\tif (TestSettings.SessionTestsFailed) Assert.Fail("Session failed, test aborted");
    

    和 TestFinalize() 类似。

    【讨论】:

      猜你喜欢
      • 2018-07-19
      • 1970-01-01
      • 1970-01-01
      • 2021-04-17
      • 1970-01-01
      • 2016-01-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多