Can you pass data to a test fixture just like you pass data to test cases?

本小妞迷上赌 提交于 2019-12-10 17:49:27

问题


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)


回答1:


Absolutely!

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)

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

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.



来源:https://stackoverflow.com/questions/38079476/can-you-pass-data-to-a-test-fixture-just-like-you-pass-data-to-test-cases

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