views:

160

answers:

3

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.

+2  A: 

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...

Blair Conrad
After looking at the NUnit code, I think this is the best we can do without modifying NUnit. Thanks for your answer.
Wesley Wiser
A: 

I thought I had an elegant solution to this, but alas, did not work as I expected. I was hoping (and maybe you can with more effort) to derive from the IgnoreAttribute class. I thought this would work:

[Test, BugId("411")]
public void TestMethod()
{
    // your test
}

public class BugIdAttribute : IgnoreAttribute
{
    private string id;

    public BugIdAttribute(string id) : base("Ignored because it is bug #" + id)
    {
        this.id = id;
    }
}

But it seems there's more to it than this. Sorry for posting an answer that is not actually an answer, but I think it's a good stepping stone for somebody who knows more about the internals of nunit than myself.

Chris Missal
I don't want to ignore the test. I want to be able to see that the bug has been fixed and run all associated tests with that bug.
Wesley Wiser
+1  A: 

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"

Wesley Wiser