/Xunit.EnumMemberData

EnumMemberData attribute to make it easy to write parameterized tests with enum values.

Primary LanguageC#MIT LicenseMIT

Xunit.EnumMemberData

Nuget Status

Xunit.EnumMemberData is a library containing one attribute ([EnumMemberData]) to simplify using enums when writing theories (parameterized unittests) using xunit.

By type of enum

[Theory]
[EnumMemberData(typeof(Gender))]
public void TestWithAllEnumValues_UsingEnumMemberData_ByType(Gender gender)
{
    _outputWriter.WriteLine($"Gender: {gender}");
}

snippet source | anchor

By property (or method) with simple return value

[Theory]
[EnumMemberData(nameof(Genders))]
public void TestWithAllEnumValues_UsingEnumMemberData_ByProperty(Gender gender)
{
    _outputWriter.WriteLine($"Gender: {gender}");
}

// Normally, xunit uses return values like IEnumerable<object[]>
public static IEnumerable<Gender> Genders
{
    get
    {
        return Enum.GetValues(typeof(Gender)).Cast<Gender>();
    }
}

snippet source | anchor