liveBook · Manning

InlineData

As explained in ASP.NET Core in Action, 3e

InlineData is an xUnit attribute that supplies literal argument values for an individual theory test case.

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

How ASP.NET Core in Action, 3e covers it

How deep it goes
Explains declaration, argument matching, and separate test discovery, while deferring MemberData and ClassData details.
Examples
Demonstrates currency conversion with zero, ordinary, and fractional values passed through three InlineData attributes.
What it assumes
Assumes basic C# test methods and assertions while establishing the distinction between Fact and Theory tests.
What you can do after
Apply it via xUnit to create parameterized tests whose individual data sets run and report separately.
How it connects
Connects Theory tests with Fact methods, assertions, MemberData, and ClassData.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of ASP.NET Core in Action, 3e. The section links point to the parts of the book it draws on.