Test parameterization in xUnit.net is similar to NUnit

Are there any tools in the xUnit.net infrastructure that are similar to the following NUnit features?

[Test, TestCaseSource("CurrencySamples")] public void Format_Currency(decimal value, string expected){} static object[][] CurrencySamples = new object[][] { new object[]{ 0m, "0,00"}, new object[]{ 0.0004m, "0,00"}, new object[]{ 5m, "5,00"}, new object[]{ 5.1m, "5,10"}, new object[]{ 5.12m, "5,12"}, new object[]{ 5.1234m, "5,12"}, new object[]{ 5.1250m, "5,13"}, // round new object[]{ 5.1299m, "5,13"}, // round } 

This will lead to the creation of 8 separate tests in the NUnit GUI

 [TestCase((string)null, Result = "1")] [TestCase("", Result = "1")] [TestCase(" ", Result = "1")] [TestCase("1", Result = "2")] [TestCase(" 1 ", Result = "2")] public string IncrementDocNumber(string lastNum) { return "some"; } 

This will generate 5 separate tests and automatically compare the results ( Assert.Equal() ).

 [Test] public void StateTest( [Values(1, 10)] int input, [Values(State.Initial, State.Rejected, State.Stopped)] DocumentType docType ){} 

This will give 6 combinatorial tests. Priceless.

A few years ago, I tried xUnit and loved it, but it lacked these features. Can not live without them. Something has changed?

+62
c # unit-testing nunit
Feb 02 2018-12-12T00:
source share
5 answers

xUnit offers a way to run parameterized tests through something called data theories . The concept is equivalent to the one found in NUnit, but the functionality you get out of the box is not so complete.

Here is an example:

 [Theory] [InlineData("Foo")] [InlineData(9)] [InlineData(true)] public void Should_be_assigned_different_values(object value) { Assert.NotNull(value); } 

In this example, xUnit will run the Should_format_the_currency_value_correctly test once for each InlineDataAttribute , passing the specified value as an argument each time.

Data theories are an extensibility point that you can use to create new ways to run your parameterized tests. The way this is done is to create new attributes that check and optionally act on the arguments and return the values โ€‹โ€‹of the test methods.

You can find a good practical example of how xUnit data theories can be extended to AutoFixture AutoData and InlineAutoData .

+88
Feb 02 '12 at 10:21
source share

Let me drop another sample here, in case this helps someone.

 [Theory] [InlineData("goodnight moon", "moon", true)] [InlineData("hello world", "hi", false)] public void Contains(string input, string sub, bool expected) { var actual = input.Contains(sub); Assert.Equal(expected, actual); } 
+32
Apr 11 '15 at 0:48
source share

Upon your first request, you can follow the examples given here .

You can build a static class containing the data needed for a collection of tests

 using System.Collections.Generic; namespace PropertyDataDrivenTests { public static class DemoPropertyDataSource { private static readonly List<object[]> _data = new List<object[]> { new object[] {1, true}, new object[] {2, false}, new object[] {-1, false}, new object[] {0, false} }; public static IEnumerable<object[]> TestData { get { return _data; } } } } 

Then, using the MemberData attribute, define the test as such

 public class TestFile1 { [Theory] [MemberData("TestData", MemberType = typeof(DemoPropertyDataSource))] public void SampleTest1(int number, bool expectedResult) { var sut = new CheckThisNumber(1); var result = sut.CheckIfEqual(number); Assert.Equal(result, expectedResult); } } 

or if you are using C # 6.0,

 [Theory] [MemberData(nameof(PropertyDataDrivenTests.TestData), MemberType = typeof(DemoPropertyDataSource))] 

The first argument to MemberDataAttribute allows you to specify the member that you use as the data source, so you have sufficient flexibility for reuse.

+8
Dec 19 '16 at 13:03
source share

I found a library that creates equivalent functionality for the NUnit [Values] attribute called Xunit.Combinatorial :

It allows you to specify parameter level values:

 [Theory, CombinatorialData] public void CheckValidAge([CombinatorialValues(5, 18, 21, 25)] int age, bool friendlyOfficer) { // This will run with all combinations: // 5 true // 18 true // 21 true // 25 true // 5 false // 18 false // 21 false // 25 false } 

Or you can implicitly determine the minimum number of calls for all possible combinations:

 [Theory, PairwiseData] public void CheckValidAge(bool p1, bool p2, bool p3) { // Pairwise generates these 4 test cases: // false false false // false true true // true false true // true true false } 
+3
May 14 '17 at 9:23 a.m.
source share

I took all the answers here and additionally used the XUnit TheoryData<,> generic types to give me a simple, easy to read and type of safe data definition for the MemberData attribute in my test, as in this example

 /// must be public & static for MemberDataAttr to use public static TheoryData<int, bool, string> DataForTest1 = new TheoryData<int, bool, string> { { 1, true, "First" }, { 2, false, "Second" }, { 3, true, "Third" } }; [Theory(DisplayName = "My First Test"), MemberData(nameof(DataForTest1))] public void Test1(int valA, bool valB, string valC) { Debug.WriteLine($"Running {nameof(Test1)} with values: {valA}, {valB} & {valC} "); } 

Three test runs observed in the test conductor for

NB Using VS2017 (15.3.3), C # 7 and XUnit 2.2.0 for .NET Core

+1
Sep 05 '17 at 9:23
source share



All Articles