Skip to content

Instantly share code, notes, and snippets.

@Hereigo
Created June 5, 2018 16:21
Show Gist options
  • Save Hereigo/d35aab23df59df8d1df84c1a77812ce7 to your computer and use it in GitHub Desktop.
Save Hereigo/d35aab23df59df8d1df84c1a77812ce7 to your computer and use it in GitHub Desktop.
xUnit Theory MemberData
using System.Collections;
using System.Collections.Generic;
using Xunit;
public class Person
{
public string Name { get; set; }
public int Age { get; set; }
}
public class TestDataGenerator
{
public static IEnumerable<object[]> GetPersonFromDataGenerator()
{
yield return new object[]
{
new Person {Name = "Tribbiani", Age = 56},
new Person {Name = "Gotti", Age = 16},
new Person {Name = "Sopranos", Age = 15},
new Person {Name = "Corleone", Age = 27}
};
yield return new object[]
{
new Person {Name = "Mancini", Age = 79},
new Person {Name = "Vivaldi", Age = 16},
new Person {Name = "Serpico", Age = 19},
new Person {Name = "Salieri", Age = 20}
};
}
}
public class ParameterizedTests
{
[Theory]
[MemberData(nameof(TestDataGenerator.GetPersonFromDataGenerator), MemberType = typeof(TestDataGenerator))]
public void AllPersons_AreAbove14_WithMemberData_FromDataGenerator(Person a, Person b, Person c, Person d)
{
Assert.True(IsAboveFourteen(a));
Assert.True(IsAboveFourteen(b));
Assert.True(IsAboveFourteen(c));
Assert.True(IsAboveFourteen(d));
}
public bool IsAboveFourteen(Person person)
{
return person.Age > 14;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment