简体   繁体   中英

How to ignore particular test case in MSTest

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:

在此处输入图像描述

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

How particular test case can be ignored in MSTest?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM