1

I try to write a test that looks like this:

private static readonly IEnumerable<MyEnumType> ListOfEnumValues = Enum.GetValues(typeof(MyEnumType)).Cast<DocumentType>();
private static readonly IEnumerable<bool> ListOfBoolValues = new List<bool>(){true, false};

    
[Theory]
[TestData(ListOfEnumValues, ListOfBooleanValues)]
public void Test(EnumType type, bool myBool)
{
  // test code
};

How is this possible?

2 Answers 2

2

Here is a possible solution:

public static IEnumerable<object[]> ListOfValues =>
    from EnumType et in Enum.GetValues(typeof(EnumType))
    from bool myBool in new[] { true, false }
    select new object[] { et, myBool };


[Xunit.Theory]
[MemberData(nameof(ListOfValues))]
public void Test(EnumType type, bool myBool)
{
    // test code
};

This will generate tests for all possible combinations.

If the enum has 3 values, and we have a bool like in the example, this would generate 6 (3 x 2) tests

1

You can take advantage of the yield return:

public static IEnumerable<object[]> ListOfValues()
{
     foreach (var et in Enum.GetValues(typeof(EnumType)))
     {
        yield return new [] { et, true };
        yield return new [] { et, false };
     }
}

IMHO this approach might be a bit easier to understand than the multi source linq query expression.

Not the answer you're looking for? Browse other questions tagged or ask your own question.