Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are Custom Filters in NUnit Possible?

Tags:

filter

nunit

Is it possible to define a custom filter so that NUnit will only run specific tests? I have many of my Nunit tests marked with a custom attribute "BugId". Is it possible to write a filter so that I can pass in a number and only run the tests with that attribute and number? If so show mockup or real code.

like image 825
Wesley Wiser Avatar asked Aug 13 '09 15:08

Wesley Wiser


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. Both the gui and console test runners allow specifying a list of categories to be included in or excluded from the run.


2 Answers

Do the filters need to use your custom attribute, or could you use an NUnit Category? Something like

[Test]
[Category("BugId-12234")]
public void Test()
{
  ....
}

... and then use the /include=STR flag:

nunit-console /include=BugId-12234 ...

? I'd recommend subclassing Category to make your custom attribute, but I don't think that allows you to add a switchable parameter to your attribute...

like image 183
Blair Conrad Avatar answered Jan 06 '23 20:01

Blair Conrad


Starting with NUnit 2.4.6, the NUnit attributes are not sealed and subclasses will be recognized as their base classes. Thus:

public class BugId : TestAttribute
{
    public BugId(int bugNumber) : base("Test for Bug #" + bugNumber) { }
}

[BugId(1)]
public void Test() {}

can be called on the command line like this:

nunit-console /include="Test for Bug #1"

like image 39
Wesley Wiser Avatar answered Jan 06 '23 21:01

Wesley Wiser