* n *测试失败后如何停止MsTest测试执行 [英] How to stop MsTest tests execution after *n* failed tests

查看:90
本文介绍了* n *测试失败后如何停止MsTest测试执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过MS Test(从Windows控制台)运行单元测试,以便当失败的测试计数超过特定阈值时,我可以停止/终止测试执行.

I want to run unit tests via MS Test (from windows console) in a way that I can stop/kill the test execution whenever the failed tests count exceeds certain threshold value.

在我的用例中,当一定比例的测试已失败时,没有必要继续运行测试.

For my use case there is no point to keep running tests when certain percentage of the tests already failed.

我只能考虑创建一个新的控制台应用程序来包装mstest.exe执行,因此可以实时解析标准输出, 并最终终止了该过程,例如:

I can only think in creating a new console app to wrap the mstest.exe execution, so I can parse the standard output in real-time, and eventually kill the process, for example:

var pi = new ProcessStartInfo()
{
    FileName = MS_TEST,
    UseShellExecute = false,
    RedirectStandardOutput = true,
    Arguments = MS_TEST_ARGS
};
int passed = 0; int failed = 0;
using (var process = Process.Start(pi))
{
    while (!process.StandardOutput.EndOfStream)
    {
        string line = process.StandardOutput.ReadLine();
        if (line.Contains("Passed"))
            passed++;
        if (line.Contains("Failed"))
            failed++;
        if (failed >= THRESHOLD)
        {
            process.Kill();
            break;
        }
    }
}

有人可以建议一种更好的方法吗?我认为这不是MsTest本身支持的.

Can anyone suggest a better way for doing this? I don't think this is natively supported by MsTest.

PowerShell似乎是一个选项,但是stdout重定向并不简单.

PowerShell seems to be an option, but the stdout redirect is not trivial.

请注意,我无法修改测试代码,我需要在不进行任何方式修改测试代码的情况下完成此操作.

As a note, I cannot modify the test code, I need this to be done without modifying the tests code in any way.

推荐答案

创建一个BaseTestClass,其中包含负责终止运行测试的进程的方法.

Create a BaseTestClass which contains a method responsible for killing the process that runs the tests.

using System.Diagnostics;

namespace UnitTestProject1
{
    public class BaseTestClass
    {
        private readonly int _threshold = 1;
        private static int _failedTests;

        protected void IncrementFailedTests()
        {
            if (++_failedTests >= _threshold)
                Process.GetCurrentProcess().Kill();
        }
    }
}

您必须从BaseTestClass继承所有测试类,并使用[TestCleanup]属性.当DemoTests类中定义的测试完成运行时,将评估TestCleanup()方法.在这种方法中,我们评估刚刚完成的测试的输出.如果失败,我们将终止负责运行测试的进程.

Your must inherit all your test classes from BaseTestClass and use the [TestCleanup] attribute. The TestCleanup() method is evaluated when a test defined in the DemoTests class has finished running. Is in that method where we evaluate the output of the test that has just finished. If it failed, we kill the process responsible for running the tests.

在下面的示例中,我们定义了三个测试.第二项测试Test_Substracting_Operation()旨在故意失败,因此第三项测试将永远不会运行.

In the following example we have defined three tests. The second test, Test_Substracting_Operation(), is intended to fail intentionally, so the third test will never be run.

using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace UnitTestProject1
{
    [TestClass]
    public class DemoTests : BaseTestClass
    {
        public TestContext TestContext { get; set; }

        [TestCleanup]
        public void TestCleanup()
        {
            if (TestContext.CurrentTestOutcome == UnitTestOutcome.Failed)
            {
                IncrementFailedTests();
            }
        }
        [TestMethod]
        public void Test_Adding_Operation()
        {
            // Arrange
            int x = 1;
            int y = 2;

            // Act
            int result = x + y;

            // Assert
            Assert.AreEqual(3, result);
        }

        [TestMethod]
        public void Test_Substracting_Operation()
        {
            // Arrange
            int x = 1;
            int y = 2;

            // Act
            int result = x - y;

            // Assert
            Assert.AreEqual(100, result);
        }

        [TestMethod]
        public void Test_Multiplication_Operation()
        {
            // Arrange
            int x = 1;
            int y = 2;

            // Act
            int result = x * y;

            // Assert
            Assert.AreEqual(2, result);
        }
    }
}

这篇关于* n *测试失败后如何停止MsTest测试执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆