xUnit.net Theory where MemberData is from derived class
You're correct in your answer. Posting this non-answer in case it happens to spark an idea.
MemberData
can be passed params, which might help depending on your specific scenario?
Other than that, the best you can prob do is to put a forwarder:
public abstract class BaseTest
{
protected void RunTest(string expected, string actual)
{
Assert.Equal(expected, actual);
}
}
public class ComplexTest : BaseTest
{
static IEnumerable<object[]> Data() =
{
{ "a", "a" }
}
[Theory, MemberData(nameof(Data))]
void TestData(expected, actual) => base.RunTest(expected, actual);
}
As far as I know, this is not possible. MemberData
's data is required to be static
, therefore the data must originate from its own class.
public static IEnumerable<object[]> Data()
{
// data goes here
}
[Theory]
[MemberData(nameof(Data))]
public void TestData(string expected, string actual)
{
// assert goes here
}
Another way of doing this (and IMO cleaner), is to put your test scenarios in their own specific classes and just define each scenario set as a separate MemberData attribute:
public class BaseTest
{
[Theory]
[MemberData(nameof(TestScenarios1.Data), MemberType = typeof(TestScenarios1)]
[MemberData(nameof(TestScenarios1.MoreData), MemberType = typeof(TestScenarios1)]
[MemberData(nameof(TestScenarios2.DifferentData), MemberType = typeof(TestScenarios2)]
public void TestData(string expected, string actual)
{
// assert goes here
}
}
public class TestScenarios1
{
public static IEnumerable<object[]> Data()
{
// data goes here
}
public static IEnumerable<object[]> MoreData()
{
// data goes here
}
}
public class TestScenarios2
{
public static IEnumerable<object[]> DifferentData()
{
// data goes here
}
}