Fetch Params from Nunit3 in C# test fixture

こ雲淡風輕ζ 提交于 2020-01-15 06:44:43

问题


I am using the params argument provided in the Nunit3 version to pass multiple parameters.

However, I am unable to fetch them with the C# test fixture. I have searched but unable to get a correct result.

Can someone provide me with the pointers on how to fetch those param arguments in c#.

Any help will be appreciated. Thanks in advance.


回答1:


First, make sure that you are using both NUnit console 3.4.1 and NUnit Framework 3.4.1.

Your command line option of --params:Code=XXX --params:Date=2011-05-16 looks correct. You can also combine multiple parameters with a semicolon, --params:Code=XXX;Date=2011-05-16

To access the parameters in your unit tests, use TestContext.Parameters.Get("Code") in your tests. There is also a string Get(string key, string default) and a T Get(string key, T default) which does a Convert.ChangeType.

It isn't well documented yet, so see the pull request that implemented the feature for more information.

Here is an example test,

[Test]
public void TestCommandLineParameters()
{
    var code = TestContext.Parameters.Get("Code", "<unknown>");
    var date = TestContext.Parameters.Get("Date", DateTime.MinValue);

    TestContext.WriteLine($"Fetched test parameters {code} and {date}");
}

Which I run with the command line and NUnit 3.4.1,

nunit3-console.exe --params:Code=XXX --params:Date=2011-05-16 .\nunit-v3.dll

In the output, I see

=> nunit.v3.TestParamsTest.TestCommandLineParameters
Fetched test parameters XXX and 2011-05-16 12:00:00 AM


来源:https://stackoverflow.com/questions/38346286/fetch-params-from-nunit3-in-c-sharp-test-fixture

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