Lesson 18 of 93 · 6 min read · Updated
Fact vs Theory in xUnit: When One Example Is Enough and When You Need Many
On this page
xUnit gives you two attributes for writing tests: [Fact] and [Theory]. They look similar, but they answer different questions. A fact checks one fixed example. A theory checks that one rule holds for many inputs. This lesson shows both attributes against the same piece of business code, so you can see when each one fits and how xUnit reports the results.
Technical Requirements
- .NET 11 SDK
- xunit 2.9.3
- xunit.runner.visualstudio 4.0.1 and Microsoft.NET.Test.Sdk 18.12.0
- A class library project with the code under test and a test project that references it
The Code Under Test: a Shipping Fee Calculator
The examples in this lesson, and in the rest of this section, use a small shipping calculator. It takes an order total, a parcel weight and a shipping zone, and returns a fee. The rules form a matrix: each zone has a base fee and a per-kilo fee, the first kilo is included in the base fee, and orders of 2000 or more ship for free inside the country. International orders never ship for free.
M03.DataDriven/ShippingCalculator.cs
A rule matrix like this is the textbook case for a theory: there is one behavior, but there are many inputs that should all follow it. Writing a separate test method for every cell of the matrix would copy the same Arrange-Act-Assert lines over and over.
A Fact: One Representative Example
Use [Fact] when a single, fixed example tells the whole story. A fact takes no parameters. The data lives inside the method body, and the test runs once.
M03.DataDriven.Tests/FactVsTheoryTests.cs
This test says: a typical local order under one kilo costs the local base fee. One example makes that point. Adding more local orders under one kilo would not teach the reader anything new, so a fact is the right tool.
Facts are also the right choice for behavior that has no natural input dimension, such as "the constructor sets the default status" or "disposing twice does not throw". If you cannot name the parameter that would vary, you probably want a fact.
A Theory: One Rule, Many Inputs
Use [Theory] when you want to prove that the same rule holds across a set of inputs. A theory is a test method with parameters. Data attributes such as [InlineData] supply the values, and xUnit runs the method once per data row.
M03.DataDriven.Tests/FactVsTheoryTests.cs
The rule here is "a parcel of one kilo or less costs the base fee of its zone". The zone is the dimension that varies, so it becomes a parameter. The expected fee varies with it, so it becomes a second parameter. The method body stays short and states the rule only once.
Notice the test name. It describes the rule (ReturnsZoneBaseFee), not one specific value. A good theory name reads like a sentence that is true for every row.
How xUnit Reports a Theory
xUnit treats every data row as a separate test case. In Test Explorer or in the dotnet test output you see three entries for the theory above, each with its arguments in the display name, for example Calculate_ForOneKiloOrLess_ReturnsZoneBaseFee(zone: Domestic, expected: 60).
This has two practical effects:
- A theory fails if any of its rows breaks the rule. The other rows still run and still report their own result.
- When a row fails, you see exactly which input caused it. You do not need to add logging or debug to find out which case went wrong.
Compare this with a fact that loops over an array of inputs and asserts inside the loop. The loop stops at the first failure, hides the remaining results and reports one test name for all cases. A theory gives you one result per case for free.
Run the tests from the command line to see the cases:
Choosing Between Fact and Theory
A simple question helps: "Am I showing one example, or am I proving a rule?"
| Situation | Attribute |
|---|---|
| One fixed scenario explains the behavior | [Fact] |
| The behavior has no input that naturally varies | [Fact] |
| The same rule must hold for several inputs | [Theory] |
| You want one test result per input | [Theory] |
A common mistake goes the other way: turning every test into a theory with one row, or packing unrelated scenarios into one theory just because the method signatures match. Each theory should check one rule. If the rows need different assertions or different explanations, they belong in separate tests.
Summary
[Fact]runs one fixed example and takes no parameters.[Theory]runs the same test method once per data row, so one rule is checked against many inputs.- xUnit reports each theory row as its own test case, with its arguments in the name.
- A theory fails if any row fails, and you see exactly which input broke the rule.
- Name theories after the rule they check, and keep one rule per theory. The next lesson looks at
[InlineData]in detail.