Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to stop test if one test fails using vstest.console.exe?

Tags:

c#

vstest

I have a batch file that contains multiple tests defined in a similar form below.

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

The tests run in order from t1 to t5. However, I want to stop vstest if any one of the tests fails. Is this possible using vstest.console.exe?

Btw, the contents of my test.runsettings is

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

I have checked the Documentation for runsettings, it seems there is no flag/attribute for this case.

like image 325
Bangonkali Avatar asked Oct 18 '22 12:10

Bangonkali


1 Answers

If it is an option for you, then you could introduce base class for tests with cleanup, initialize methods and TestContext property.

In cleanup method you will check if test is failed and by triggering Assert.Fail in TestInitialize you don't allow any other test to pass after that.

[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!");
    }
}
like image 66
lukbl Avatar answered Oct 21 '22 03:10

lukbl