Nunit parameterised TestFixtures with parameters set at runtime?

落爺英雄遲暮 提交于 2019-12-04 12:54:14

I emailed the Nunit-Discuss list about this and got the below response from Charlie Poole. In brief, this isn't possible yet, but is being looked at for future versions of Nunit.

Hi,

Simply stated, what you want is coming, but it's not here yet. Parameterized fixtures are (as you have discovered) limited by the fact that you can only use arguments that are permitted in attributes. We'd like to have a way that allows use of properties and methods as for test cases but fixtures are a bit more complicated, since the type may be generic.

Have you considered using a generic fixture as a workaround? You could pass in the environment as a Type (or Types) and any constructor arguments as non-type arguments. I don't know your app, so take this with a grain of salt, but how about something like...

[TestFixture(typeof(Environment1), 42, "string")]
public class TestClass<T>
{
    IEnvironment env;

    public TestClass(int n, string s)
    {
       env = new T( n, s);
    }
    ...

}

Note that this is "maillistcode" so may not work. :-)

Charlie

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