问题
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