Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nunit: Add Category to specific test cases

Tags:

c#

nunit

testcase

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

like image 368
Alan Potter Avatar asked Dec 14 '15 10:12

Alan Potter


People also ask

What is category attribute in NUnit?

The Category attribute provides an alternative to suites for dealing with groups of tests. Either individual test cases or fixtures may be identified as belonging to a particular category. Some runners, including the Console Runner, allow specifying categories to be included in or excluded from the run.

How do you prioritize test cases in NUnit?

There is no facility in NUnit to order tests globally. Tests with an OrderAttribute argument are started before any tests without the attribute. Ordered tests are started in ascending order of the order argument. Among tests with the same order value or without the attribute, execution order is indeterminate.

How do I use the TestCase attribute in NUnit?

The TestCase attribute in NUnit marks a method with parameters as a test method. It also provides the inline data that needs to be used when that particular method is invoked. It can appear one or more times on the test method, with each appearance carrying values for the test case.

How do you write multiple test cases in NUnit?

If you add a second parameter with the Values attribute, for per value of the first parameter, NUnit will add a test for every value of the second parameter. Another way to avoid having to write duplicate tests when testing the same behaviour with different inputs is to use TestCase attribute on the test itself.


1 Answers

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);
    }
}
like image 80
unickq Avatar answered Oct 24 '22 23:10

unickq