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

Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.