您能否像将数据传递给测试用例一样将数据传递给测试装置? [英] Can you pass data to a test fixture just like you pass data to test cases?
问题描述
您能否像将数据传递给测试用例一样将数据传递给 NUnit3 测试装置?这样做是否有意义?(根据参数运行套件(夹具类))
Can you pass data to a NUnit3 test fixture just like you pass data to test cases? Would it even make sense to do this? (run a suite (fixture class) based on a parameter)
推荐答案
绝对!
如果你需要传入的参数数量有限,你可以将它们放在普通的[TestFixture]
属性中,然后它们会被传递给TestFixture的构造函数.例如
If there's a limited amount of parameters you need to pass in, you can just put these in the normal [TestFixture]
attribute, and they will be passed to the constructor of the TestFixture. e.g.
[TestFixture("hello", "hello", "goodbye")]
[TestFixture("zip", "zip", "zap")]
public class ParameterizedTestFixture
{
private string eq1;
private string eq2;
private string neq;
public ParameterizedTestFixture(string eq1, string eq2, string neq)
{
this.eq1 = eq1;
this.eq2 = eq2;
this.neq = neq;
}
此版本将使用两组不同的参数运行测试装置两次.(文档)
This version would run the test fixture twice, with the two different sets of parameters. (Docs)
如果您有更多参数,您可能希望查看 [TestFixtureSource]
- 它的工作方式大致相同,但允许您在静态方法中计算参数,而不是明确指定在一个属性中.(文档)诸如此类:
If you have more parameters, you may wish to look at [TestFixtureSource]
- which works much the same way, but allows you to calculate your parameters in a static method, as opposed to explicity specified in an attribute. (Docs) Something such as this:
[TestFixtureSource(typeof(FixtureArgs))]
public class MyTestClass
{
public MyTestClass(string word, int num) { ... }
...
}
class FixtureArgs: IEnumerable
{
public IEnumerator GetEnumerator()
{
yield return new object[] { "Question", 1 };
yield return new object[] { "Answer", 42 };
}
}
最后,如果您需要在运行时传入参数,这也可以通过 --params
命令行选项来实现,这是 NUnit v3.4 中的新功能.看起来这还没有被记录下来,但是您可以将它以 --params:X=5;Y=7"
格式传递到 NUnit 控制台命令行中.然后可以检索它通过 TestContext.Parameters
类.
Finally, if you need to pass parameters in at run-time, this is also possible through the --params
command line option, new in NUnit v3.4. It doesn't look like this is documented yet, but you can pass it into the NUnit console command line in the format --params:X=5;Y=7"
. It can then be retrieved through the TestContext.Parameters
class.
这篇关于您能否像将数据传递给测试用例一样将数据传递给测试装置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!