C# – Nunit: Add Category to specific test cases

cnunittestcase

I would like to run a small set of NUnit test cases as a pre-checkin sanity check, and a more comprehensive set of test cases on my on-checkin and nightly test runs.

So I had hoped I could decorate certain tests cases with the "Category" attribute, and have only those test cases run at pre-checkin time. However, that doesn't seem to work – if I include the category then all test cases are run.

Is there a way to restrict the number of test cases being run via categories?

[TestFixture]
public class TestAddition
{
    [TestCase(1, 2, 3), Category("PreCheckin")]
    [TestCase(2, 4, 6)]
    [TestCase(3, 6, 9)]
    public void AdditionPassTest(int first, int second, int expected)
    {
        var adder = new Addition();
        var total = adder.DoAdd(first, second);
        Assert.AreEqual(expected, total);
    }
}

If I try to run this:

C:\> "C:\Program files (x86)\Nunit 2.6.4\bin\nunit-console.exe" /nologo ^
     NUnitTestCase.dll /labels /include=PreCheckin
ProcessModel: Default    DomainUsage: Single
Execution Runtime: net-3.5
Included categories: PreCheckin
***** NUnitTestCase.TestAddition.AdditionPassTest(1,2,3)
***** NUnitTestCase.TestAddition.AdditionPassTest(2,4,6)
***** NUnitTestCase.TestAddition.AdditionPassTest(3,6,9)

Tests run: 3, Errors: 0, Failures: 0, Inconclusive: 0, Time: 0.0743007328107035 seconds
Not run: 0, Invalid: 0, Ignored: 0, Skipped: 0

I was wanting only the single test case (1, 2, 3) to be run

Best Answer

You use Category attribute for all tests now. Change code to this one :)

[TestFixture]
public class TestAddition
{
    [TestCase(1, 2, 3, Category = "PreCheckin")]
    [TestCase(2, 4, 6)]
    [TestCase(3, 6, 9)]
    public void AdditionPassTest(int first, int second, int expected)
    {
        var adder = new Addition();
        var total = adder.DoAdd(first, second);
        Assert.AreEqual(expected, total);
    }
}
Related Topic