Home > Mobile >  How to ignore particular test case in MSTest
How to ignore particular test case in MSTest

Time:02-20

I am trying to ignore test case by adding Ignore keyword for DataRow attribute:

[TestClass]
public class MathTests
{
    [TestMethod]
    [DataRow(1, 1, 2)]
    [DataRow(2, 2, 3), Ignore]
    public void Sum_Test(int a, int b, int expectedSum)
    {
        var sut = new Math();
        
        var sum = sut.Sum(a, b);
        
        Assert.IsTrue(sum == expectedSum);
    }
}

public class Math
{
    public int Sum(int a, int b)
    {
        return a   b;
    }
}

but it ignores the whole test:

enter image description here

  • Target: .NET 6
  • MSTest.TestFramework: v2.2.8
  • IDE: Rider

How particular test case can be ignored in MSTest?

CodePudding user response:

First of all, there is no Ignore keyword. What are you doing is just combining 2 attributes in the same line. So, your code is equivalent to:

[TestClass]
public class MathTests
{
    [TestMethod]
    [DataRow(1, 1, 2)]
    [DataRow(2, 2, 3)]
    [Ignore]
    public void Sum_Test(int a, int b, int expectedSum)
    {
        var sut = new Math();
        
        var sum = sut.Sum(a, b);
        
        Assert.IsTrue(sum == expectedSum);
    }
}

21.3 Attribute specification

Attributes are specified in attribute sections. An attribute section consists of a pair of square brackets, which surround a comma-separated list of one or more attributes. The order in which attributes are specified in such a list, and the order in which sections attached to the same program entity are arranged, is not significant. For instance, the attribute specifications [A][B], [B][A], [A, B], and [B, A] are equivalent.

What you could do:

  1. The easiest solution is just to comment particular data rows out. (or you can use precompile variables. In this case, it will be easier to enable all ignored test cases)

    PRO: This is very easy.

    CONTRA: You won't be able to see these test cases in GUI

  2. You can create 2 tests and mark one of these tests with TestCathegory attribute.

    PRO: It will be possible to see this test in GUI.

    PRO: You'll be able to execute tests with or exclude specific TestCathegory.

    CONTRA: You'll have to duplicate your test.

    CONTRA: You'll have to use command line parameters to include or exclude specific TestCathegory on your build server.

  • Related