Created
January 15, 2019 08:36
-
-
Save oguzhankircali/0e4c66ffdc21154890ac441028b3e2f1 to your computer and use it in GitHub Desktop.
xUnit Tests With Data
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class TestDataGenerator : IEnumerable<object[]> | |
| { | |
| private readonly List<object[]> _data = new List<object[]> | |
| { | |
| new object[] {5, 1, 3, 9}, | |
| new object[] {7, 1, 5, 3} | |
| }; | |
| public IEnumerator<object[]> GetEnumerator() => _data.GetEnumerator(); | |
| IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); | |
| } | |
| public class ParameterizedTests | |
| { | |
| public bool IsOddNumber(int number) | |
| { | |
| return number % 2 != 0; | |
| } | |
| [Theory] | |
| [ClassData(typeof(TestDataGenerator))] | |
| public void AllNumbers_AreOdd_WithClassData(int a, int b, int c, int d) | |
| { | |
| Assert.True(IsOddNumber(a)); | |
| Assert.True(IsOddNumber(b)); | |
| Assert.True(IsOddNumber(c)); | |
| Assert.True(IsOddNumber(d)); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class ParameterizedTests | |
| { | |
| public bool IsOddNumber(int number) | |
| { | |
| return number % 2 != 0; | |
| } | |
| [Theory] | |
| [InlineData(5, 1, 3, 9)] | |
| [InlineData(7, 1, 5, 3)] | |
| public void AllNumbers_AreOdd_WithInlineData(int a, int b, int c, int d) | |
| { | |
| Assert.True(IsOddNumber(a)); | |
| Assert.True(IsOddNumber(b)); | |
| Assert.True(IsOddNumber(c)); | |
| Assert.True(IsOddNumber(d)); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class Person | |
| { | |
| public string Name { get; set; } | |
| public int Age { get; set; } | |
| } | |
| public class TestDataGenerator : IEnumerable<object[]> | |
| { | |
| public static IEnumerable<object[]> GetNumbersFromDataGenerator() | |
| { | |
| yield return new object[] { 5, 1, 3, 9 }; | |
| yield return new object[] { 7, 1, 5, 3 }; | |
| } | |
| 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 | |
| { | |
| public bool IsOddNumber(int number) | |
| { | |
| return number % 2 != 0; | |
| } | |
| public bool IsAboveFourteen(Person person) | |
| { | |
| return person.Age > 14; | |
| } | |
| public static IEnumerable<object[]> GetNumbers() | |
| { | |
| yield return new object[] { 5, 1, 3, 9 }; | |
| yield return new object[] { 7, 1, 5, 3 }; | |
| } | |
| [Theory] | |
| [MemberData(nameof(GetNumbers))] | |
| public void AllNumbers_AreOdd_WithMemberData(int a, int b, int c, int d) | |
| { | |
| Assert.True(IsOddNumber(a)); | |
| Assert.True(IsOddNumber(b)); | |
| Assert.True(IsOddNumber(c)); | |
| Assert.True(IsOddNumber(d)); | |
| } | |
| [Theory] | |
| [MemberData(nameof(TestDataGenerator.GetNumbersFromDataGenerator), MemberType = typeof(TestDataGenerator))] | |
| public void AllNumbers_AreOdd_WithMemberData_FromDataGenerator(int a, int b, int c, int d) | |
| { | |
| Assert.True(IsOddNumber(a)); | |
| Assert.True(IsOddNumber(b)); | |
| Assert.True(IsOddNumber(c)); | |
| Assert.True(IsOddNumber(d)); | |
| } | |
| [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)); | |
| } | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks to @HamidMosalla for this #post.
http://hamidmosalla.com/2017/02/25/xunit-theory-working-with-inlinedata-memberdata-classdata/