I have the following method which generates a set of test cases!
public IEnumerable<ResultsOfCallMyMethod> PrepareTestCases(param1)
{
foreach (string entry in entries)
{
yield return callMyMethod(param1);
}
}
How can I pass param which is of type string as a parameter to my PrepareTestCases()
method?
Is there a way to do the following:
[Test, Category("Integration"), TestCaseSource("PrepareTestCases", param1)]
public void TestRun(ResultsOfCallMyMethod testData)
{
// do something!
}
I've made a change for this in the latest version of nunit which is about to be released (3.2).
https://github.com/nunit/nunit/blob/4f54fd7e86f659682e7a538dfe5abee0c33aa8b4/CHANGES.txt
- TestCaseSourceAttribute now optionally takes an array of parameters that can be passed to the source method
It is now possible to do something like this
[Test, Category("Integration"), TestCaseSource(typeof(MyDataSources),"PrepareTestCases", new object[] {param1})]
public void TestRun(ResultsOfCallMyMethod testData)
{
// do something!
}
private class MyDataSources
{
public IEnumerable<ResultsOfCallMyMethod> PrepareTestCases(param1)
{
foreach (string entry in entries)
{
yield return callMyMethod(param1);
}
}
}
If you look at TestCaseSourceAttribute doc you will see there is no any way to pass the parameter to the method which returns test cases.
The method, which generates test cases, should be parameterless.
So, assuming you are going to avoid code duplication and you need to reuse the same method to generate a few test case lists, I'd recommend you to do the following:
Write parameterized method which actually generates test cases sets:
(PrepareTestCases()
already does that)
public IEnumerable<ResultsOfCallMyMethod> PrepareTestCases(string param)
{
foreach (string entry in entries)
{
yield return CallMyMethod(param);
}
}
Write parameterless wrappers which call test cases generator and pass desired parameter there:
public IEnumerable<ResultsOfCallMyMethod> PrepareTestCases_Param1()
{
return PrepareTestCases("param1");
}
public IEnumerable<ResultsOfCallMyMethod> PrepareTestCases_Param2()
{
return PrepareTestCases("param2");
}
Write test methods and pass paremeterless wrappers there as test case sources:
[TestCaseSource("PrepareTestCases_Param1")]
public void TestRun1(ResultsOfCallMyMethod data)
{
}
[TestCaseSource("PrepareTestCases_Param2")]
public void TestRun2(ResultsOfCallMyMethod data)
{
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With