在运行时设置参数的Nunit参数化TestFixtures? [英] Nunit parameterised TestFixtures with parameters set at runtime?

查看:149
本文介绍了在运行时设置参数的Nunit参数化TestFixtures?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对能够通过在运行时通过静态方法或返回IEnumerable的属性传递给它的构造函数实例化多个testfixture感兴趣.

I'm interested in being able to instantiate multiple testfixtures with constructor arguments passed to it at runtime by a static method or property returning an IEnumerable.

在Nunit 2.5中,他们引入了参数化测试和测试装置.这些使您可以编写一个测试,并使用TestCase属性提供的多个输入运行它,并编写一个测试类,并分别使用不同的构造函数参数实例化它的多个实例.

In Nunit 2.5 they introduced parameterised tests and test fixtures. These allow you to write a single test and run it with several inputs provided using the TestCase attribute, and write a single test class and instantiate multiple instances of it with different constructor arguments respectively.

除此之外,还可以使用TestCaseSource属性基于属性或方法的输出创建多个测试用例.这将使用实现IEnumerable的方法/属性的输出来创建一组测试用例,列表中的每个对象一个.这是我想做的,但是在夹具级别而不是测试级别.

In addition to this, it is possible to create several test cases based on the output of a property or method, using the TestCaseSource attribute. This will use the output of a method/property that implements IEnumerable to create a set of testcases, one per object in the list. This is what I'd like to be able to do, but at the fixture level not the Test level.

我的用例的一些背景:

我正在测试仿真软件,并且必须运行(从序列化对象中)加载的仿真环境",然后才能运行任何仿真.大约有5种不同类型的sim,因此我的测试课程有5种测试方法(每种类型的sim都有一种).我目前正在使用继承(而不是参数化夹具)在几个(大约六打)模拟环境下运行测试用例,这些环境是从生产数据中提取的.

I'm testing simulation software, and a 'simulation environment' that must be loaded (from a serialized object) before any simulations can be run. There are about 5 different types of sim, so my test class has 5 test methods (one for each type of sim). I'm using inheritance currently (instead of parameterised fixtures) to run the test cases under several (a half dozen or so) simulation environments, which have been taken from production data.

我的麻烦源于以下事实:在最近一次尝试增加代码覆盖率的过程中,我们自动生成了模拟组件的所有可能组合,从而产生了100多个模拟环境.我不想为每个继承类创建继承的类,所以我将TestCaseSource与一个属性一起使用,该属性返回文件夹中的所有工作区,并修改测试,以便它们在测试本身内(重新)加载sim环境.每个测试用例.

My trouble springs from the fact that in a recent attempt to increase code coverage, we automatically generated all possible combinations of simulation components, resulting in 100+ sim environments. I don't want to create inherited classes for each of these so instead I'm using TestCaseSource with a property that returns all the workspaces in a folder, and modifying the tests so they (re)load the sim environment within the test itself for each testcase.

理想情况下,我想在每个仿真环境中拥有一个灯具,并确定它们在运行时有多少个.我知道我可以通过将sim环境路径硬编码为100+个TestFixture属性来实现前者,我可以做到后者吗?

Ideally I'd like to have one fixture per simulation environment, and determine how many/what these are at runtime. I know I can do the former via hardcoding the sim environment paths into 100+ TestFixture attributes, can I do the latter?

推荐答案

我通过电子邮件发送了 Nunit-讨论列表,并从 Charlie Poole 中得到以下答复.简而言之,这还不可能,但是正在考虑Nunit的未来版本.

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.

简单地说,您想要的是 来了,但是还没来. 参数化的灯具是 被事实所限制 只能使用 允许在属性中使用.我们想要 有一种方法可以使用 特性和测试方法 情况,但固定装置要多一些 复杂,因为类型可能是 通用的.

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);
    }
    ...

}

请注意,这是"maillistcode",因此 可能不起作用. :-)

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

查理

这篇关于在运行时设置参数的Nunit参数化TestFixtures?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆