Are there any options to pass data from non-static TestCaseSource or ValueSource?
I want to pass some data to tests from an injected dbContext so I can't use static sources.
The following code throws an error: "The sourceName specified on a ValueSourceAttribute must refer to a non-null static field, property or method."
[Parallelizable(ParallelScope.All)]
[FixtureLifeCycle(LifeCycle.InstancePerTestCase)]
public class MyTests
{
public MyTests()
{
}
public IEnumerable ValueSource
{
get
{
yield return new TestCaseData("test1");
yield return new TestCaseData("test2");
// yield some data from DbContext
}
}
[Test]
public void MyTest([ValueSource(nameof(ValueSource))] string name)
{
Console.WriteLine(name);
}
}
That doesn't work either.
[Parallelizable(ParallelScope.All)]
public class MyTests
{
private static List<TestCaseData> additional = new List<TestCaseData>();
[OneTimeSetUp]
public void OneTimeSetUp()
{
additional.Add(new TestCaseData("test3"));
}
public static IEnumerable<TestCaseData> TestCases
{
get
{
yield return new TestCaseData("test1");
yield return new TestCaseData("test2");
foreach (var testCase in additional)
yield return testCase;
}
}
[Test, TestCaseSource(nameof(TestCases))]
public void MyTest(string name)
{
Console.WriteLine(name);
}
}