Nunit 参数化 TestFixtures 并在运行时设置参数?

发布于 2024-09-17 07:15:03 字数 759 浏览 6 评论 0原文

我感兴趣的是能够使用在运行时通过返回 IEnumerable 的静态方法或属性传递给它的构造函数参数来实例化多个测试装置。

在 Nunit 2.5 中,他们引入了参数化测试和测试夹具。这些允许您编写单个测试并使用使用 TestCase 属性提供的多个输入运行它,并编写单个测试类并分别使用不同的构造函数参数实例化它的多个实例。

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


我的用例的一些背景:

我正在测试模拟软件,以及在运行任何模拟之前必须加载(从序列化对象)的“模拟环境”。大约有 5 种不同类型的 sim,因此我的测试类有 5 种测试方法(每种类型的 sim 各一个)。我当前正在使用继承(而不是参数化装置)在几个(六个左右)模拟环境下运行测试用例,这些环境是从生产数据中获取的。

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

理想情况下,我希望每个模拟环境都有一个夹具,并在运行时确定这些夹具的数量/内容。我知道我可以通过将 sim 环境路径硬编码为 100 多个 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.

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.

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.


Some background on my use case:

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.

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.

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小…红帽 2024-09-24 07:15:03

我向 Nunit-Discuss 列表发送了有关此问题的电子邮件,并得到了 查理·普尔。简而言之,这目前还不可能,但 Nunit 的未来版本正在考虑这一点。

嗨,

简单来说,你想要的是
即将到来,但还没有到来。
参数化夹具是(如您
已经发现)受事实限制
你只能使用这样的参数
属性中允许。我们想要
有一种方法允许使用
测试的属性和方法
案例但固定装置有点多
复杂,因为类型可能是
通用。

您是否考虑过使用泛型
固定装置作为解决方法?你可以
作为类型传递到环境中(或
类型)和任何构造函数参数
作为非类型参数。我不知道
你的应用程序,所以请稍微考虑一下
盐,但是像...这样的东西怎么样?

[TestFixture(typeof(Environment1), 42, "string")]
公共类 TestClass;
{
    I环境env;

    公共 TestClass(int n, 字符串 s)
    {
       env = 新 T( n, s);
    }
    ...

}

请注意,这是“maillistcode”,因此
可能不起作用。 :-)

查理

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文