I have a sample class Plant, which has function 'bool IsEqual(Plant plant);'. We will create a Theory test for it.
Class IsEqualTestData is a ClassData used with complex type Plant. It generates a list of arrays of 3 objects Plant1, Plant2, and ExpectedResult boolean.[Theory] [ClassData(typeof(IsEqualTestData))] public void IsEqual(Plant plant1, Plant plant2, bool expectedResult) { Assert.Equal(expectedResult, plant1.IsEqual(plant2)); }
When running in xUnit it will report result of running each entry in the test.private class IsEqualTestData : IEnumerable<object[]> { private readonly List<object[]> _data = new List<object[]> { new object[] { new Plant() { Name = PlantTester.PlantName1, Description = PlantTester.PlantDescription1 }, new Plant() { Name = PlantTester.PlantName2, Description = PlantTester.PlantDescription2 }, false }, new object[] { new Plant() { Name = PlantTester.PlantName1 }, new Plant() { Name = PlantTester.PlantName1 }, true }, new object[] { new Plant() { Name = PlantTester.PlantName1, Description = PlantTester.PlantDescription1 }, new Plant() { Name = PlantTester.PlantName2, Description = PlantTester.PlantDescription1 }, true } }; public IEnumerator<object[]> GetEnumerator() { return _data.GetEnumerator(); } IEnumerator IEnumerable.GetEnumerator() { return GetEnumerator(); } }
If you are using Visual Studio you can download snippet file from github repository.
Reference:
xUnit_ClassData.snippetUsing dotnet watch test for continuous testing with .NET Core and XUnit.net - Scott Hanselman
Great Blog ... thanks for sharing.
ReplyDeleteweb development & hosting in India
web developer Brisbane