InlineData is an xUnit attribute for supplying literal argument values to a Theory test. Each attribute instance provides the complete argument set for one test run, allowing one test method to check several cases. section 35.5
Use InlineData when the cases can be written directly on the test method.
- Mark the method with Theory and declare parameters matching the InlineData values. section 35.5
- Add one InlineData attribute for each input and expected-result combination. section 35.5
- Run the test with dotnet test or Visual Studio; each data set appears as a separate test. section 35.5
- Choose MemberData or ClassData when the theory data should come from a static property or a separate class. section 35.5
Listing 35.5 Theory test for ConvertToGbp testing multiple sets of values
[Theory] #1 [InlineData(0, 3, 0)] #2 [InlineData(3, 1.5, 2)] #2 [InlineData(3.75, 2.5, 1.5)] #2 public void ConvertToGbp_ConvertsCorrectly ( #3 decimal value, decimal rate, decimal expected) #3 { var converter = new CurrencyConverter(); int dps = 4; #4 var actual = converter.ConvertToGbp(value, rate, dps); #5 Assert.Equal(expected, actual); #6 } #1 Marks the method as a parameterized test #2 Each [InlineData] attribute provides all the parameters for a single run of the test method. #3 The method takes parameters, which are provided by the [InlineData] attributes. #4 The dps variable doesn’t change, so there’s no need to include it in [InlineData]. #5 Executes the SUT #6 Verifies the result
A currency-conversion theory uses three InlineData rows to test zero, a normal conversion, and a fractional result. section 35.5
