New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Update documentation to clarify passing parameters to test cases #4112
Comments
@aaronfranke Can you provide a more complete example ? The following gives me 4 test cases (whereof 2 fails) public class Tests
{
[TestCaseSource(nameof(GetData))]
public void TestArrayLengthLargerThanTwo(int[] myArray)
{
Assert.That(myArray.Length, Is.GreaterThan(2));
}
public static IEnumerable<int[]> GetData()
{
yield return new[] { 1, 2, 3 };
yield return new[] { 1 };
yield return new[] { 2, 4, 6 };
yield return new[] { 3 };
}
} |
@mikkelbu Here are two examples that replicate the problem I'm facing: using NUnit.Framework;
class ExampleClass
{
public ExampleClass() { }
}
[TestFixture]
class ExampleClassTest
{
private static ExampleClass[] ExampleTestData()
{
ExampleClass[] array = new ExampleClass[2];
array[0] = new ExampleClass();
array[1] = new ExampleClass();
return array;
}
[Test]
[TestCaseSource(nameof(ExampleTestData))]
public void ExampleTest(ExampleClass[] data)
{
Assert.That(true);
}
}
using NUnit.Framework;
using System.Collections.Generic;
class ExampleClass
{
public ExampleClass() { }
}
[TestFixture]
class ExampleClassTest
{
private static List<ExampleClass> ExampleTestData()
{
List<ExampleClass> list = new List<ExampleClass>();
list.Add(new ExampleClass());
list.Add(new ExampleClass());
return list;
}
[Test]
[TestCaseSource(nameof(ExampleTestData))]
public void ExampleTest(List<ExampleClass> data)
{
Assert.That(true);
}
}
|
Based on your example I think I see what's wrong. NUnit is pulling out each element of the returned List and using that as test case data. The second example I posted works if I change the test data method to this: private static IEnumerable<List<ExampleClass>> ExampleTestData() {
List<ExampleClass> list = new List<ExampleClass>();
list.Add(new ExampleClass());
list.Add(new ExampleClass());
return new List<List<ExampleClass>> { list };
} The documentation for |
Documentation PRs are welcome. The online documentation specifies all the requirements https://docs.nunit.org/articles/nunit/writing-tests/attributes/testcasesource.html
|
I am using NUnit 3.13.3 and NUnit3TestAdapter 4.2.1 with .NET Framework 4.7.2 and Visual Studio 2019.
I am not able to pass arrays or lists. I have the method return type set to an array/list, and so is the return value and the parameter type. When I try, I get one of these error messages:
I also tried setting the return type to
TestCaseData
and wrapping innew TestCaseData(retval)
but that doesn't work either. The only solution I have found is to set the return type toTestCaseData[]
and wrap like this:This issue seems to be the same as #1327 and it looks like there has been two rounds of attempted fixes but it still doesn't work.
The text was updated successfully, but these errors were encountered: