Lesson 19 of 93 · 5 min read · Updated
InlineData in xUnit: Keeping Small Test Cases Next to the Test
On this page
[InlineData] is the simplest way to feed a theory. Each attribute is one data row, written directly above the test method. This lesson shows how to pick rows that sample the edges of a rule, how to pass null, and why [InlineData] only accepts compile-time constants. Knowing that limit tells you when to move on to other data sources.
Technical Requirements
- .NET 11 SDK
- xunit 2.9.3
- xunit.runner.visualstudio 4.0.1 and Microsoft.NET.Test.Sdk 18.12.0
- The
ShippingCalculatorclass from the previous lesson and thePasswordPolicyclass shown below
Sampling Around a Boundary
The shipping calculator includes the first kilo in the base fee and rounds the weight up to the next whole kilo. A local parcel of 1.0 kg costs 30. A parcel of 1.01 kg is billed as 2 kg, so it costs 32. That jump at one kilo is exactly where bugs hide, so the data rows sit on both sides of it.
M03.DataDriven.Tests/InlineDataTests.cs
Four rows are enough here. The first two pin the boundary. The third proves that 1.01 kg and 2.0 kg are billed the same. The last row checks the general formula with a value far from the edge. The short comments on each row explain why the row exists, which helps the next reader decide whether a row can be removed.
Look at the parameter types. The second argument is written as an integer literal (30), but the method parameter is decimal. xUnit converts compatible values when it binds arguments, so the row still works. You cannot write 30m in an attribute, because decimal is not a valid attribute argument type in C#. The same conversion lets the next test pass 1_999.99 to a decimal parameter.
Testing a Threshold
The free shipping rule has a threshold of 2000. A good set of rows puts one value just below the threshold, one exactly on it and one clearly above it.
M03.DataDriven.Tests/InlineDataTests.cs
The row 2_000.00, true is the one that matters most. It sits exactly on the edge, where the two branches of the rule meet. It proves that the code uses >= and not >. If someone changes the comparison, this single row fails and its name shows the exact input.
The expected value is a bool here, not a fee. The rule under test is "free or not", so the assertion checks that and nothing more. The exact fee for paid orders is covered by other tests. Keeping each theory focused on one question makes a failure easier to read: when this test breaks, you know the threshold logic changed, not the tariff.
Passing null and Empty Strings
The second class under test scores password strength. Short, empty and missing passwords are always weak.
M03.DataDriven/PasswordPolicy.cs
M03.DataDriven.Tests/InlineDataTests.cs
null is a valid attribute argument, so [InlineData(null)] works. With nullable reference types enabled, declare the parameter as string?. Otherwise the compiler and the xUnit analyzers warn you that a non-nullable parameter receives null. The four rows cover the three branches of string.IsNullOrWhiteSpace plus a non-empty string shorter than eight characters.
The Limit: Only Compile-Time Constants
[InlineData] is a C# attribute, and attribute arguments must be compile-time constants. That means you can use:
- numeric primitives such as
int,long,doubleandbool stringandchar- enum values, such as
ShippingZone.Local typeof(...)expressionsnull, and one-dimensional arrays of the types above
You cannot write [InlineData(new Coupon("SAVE10", 10))], and you cannot use decimal, DateTime or Guid literals. A common mistake is to work around this with strings, for example [InlineData("2026-03-14")] and a DateTime.Parse call inside the test. That moves parsing logic into the test and hides type errors until runtime.
When a row needs a real object, or when the rows are computed, switch to [MemberData] or [ClassData]. The next lessons cover both.
| Need | Use |
|---|---|
| 2 to 5 simple rows that should stay next to the test | [InlineData] |
| Complex objects or rows produced by code | [MemberData] |
| A data set shared by several test classes | [MemberData(..., MemberType = typeof(X))] |
| A large set that deserves its own class | [ClassData] with TheoryData<T...> |
Summary
- Each
[InlineData]attribute is one row of a theory, written next to the test method. - Choose rows around boundaries and thresholds: just below, exactly on, and above.
- Add a short comment to a row when its purpose is not obvious.
- Use a nullable parameter (
string?) to passnullthrough[InlineData]. [InlineData]only accepts compile-time constants, so objects,decimal,DateTimeand computed data need[MemberData]or[ClassData].