Are Custom Filters in NUnit Possible?

谁说胖子不能爱 提交于 2020-01-01 09:04:12

问题


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.


回答1:


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




回答2:


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"




回答3:


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.



来源:https://stackoverflow.com/questions/1272803/are-custom-filters-in-nunit-possible

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!