Nunit 参数化 TestFixtures 并在运行时设置参数?
我感兴趣的是能够使用在运行时通过返回 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我向 Nunit-Discuss 列表发送了有关此问题的电子邮件,并得到了 查理·普尔。简而言之,这目前还不可能,但 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.