Lesson 09 of 93 · 6 min read · Updated
Arrange-Act-Assert: Writing Your First Readable Unit Test in xUnit
On this page
Every unit test answers three questions: what state does the scenario start in, what single action do we run, and what result should we observe. The Arrange-Act-Assert pattern (AAA) puts those three answers in three visible blocks. This lesson writes a first xUnit test with AAA and explains the one rule that keeps the pattern useful: a test has exactly one Act.
Technical Requirements
- .NET 11 SDK
- xunit 2.9.3 (with xunit.runner.visualstudio and Microsoft.NET.Test.Sdk)
- A class library and a test project that references it, as set up in the previous lesson
The Code Under Test
The examples use a small Product class. It has a stock count and a Reserve method that takes items out of stock. The method also enforces two business rules: the quantity must be positive, and you cannot reserve more than you have.
M02.XUnitBasics/Product.cs
The class uses a primary constructor, and Stock has a private setter. The only way to change the stock from outside is to call Reserve. That is exactly the behaviour a test should check: not the field, but what the public method does to the observable state. (The exception messages mean "quantity must be positive" and "insufficient stock".)
The First Test
Here is the happy path. A product starts with 10 items, we reserve 3, and we expect 7 to remain.
M02.XUnitBasics.Tests/FirstTestTests.cs
A few details are worth noticing:
[Fact]marks a test that takes no parameters. xUnit discovers every public method with this attribute and runs it.- There is no
using Xunit;at the top. The test project adds it as a global using, soFactandAssertare always available. Assert.Equal(expected, actual)takes the expected value first. If you swap them, a failure reports the real value as "Expected" and your intended value as "Actual", which sends you in the wrong direction.- The named argument
stock: 10makes the important input visible. The other values only exist because the constructor needs them.
Arrange: Only the State the Scenario Needs
The Arrange block builds the smallest state that makes the scenario true. Here that is one product with enough stock. It is tempting to create a full catalog, several products and a customer "to be realistic". Resist that. Every extra object is something a reader must check to understand whether it matters for the result. When the test fails, a small Arrange block makes it obvious which input caused the failure.
If the Arrange block grows long in many tests, that is a signal. Either the production class needs too much setup (a design problem), or the tests need a helper method or builder. Do not hide the values that matter for the assertion inside such helpers, though. The reader should still see stock: 10 next to Reserve(3) and the expected 7.
Act: Exactly One Action
The Act block is one line: product.Reserve(3). This is the behaviour under test, and the test name says so.
The most common AAA mistake is a test with several Act steps, for example reserving 3, asserting, then reserving 10 more and asserting that it throws. Such a test checks two behaviours. When it fails, the name no longer tells you which rule broke, and the second half never runs if the first half fails. Split it into two tests: one for the happy path shown above, and one for "reserve more than stock throws", which a later lesson on exception assertions covers.
A simple check is to read the test name. Reserve_WithAvailableStock_DecreasesStock names one method, one condition and one expected outcome. If you cannot name a test in that shape, it probably does more than one thing.
Assert: The Observable Result
The Assert block checks what a caller of the class could observe. Stock is public, so Assert.Equal(7, product.Stock) checks the contract directly. You do not need reflection or access to private fields, and you should not want them. A test that reads private state breaks whenever the implementation changes, even when the behaviour stays the same.
Several assertions in one test are fine when they describe one outcome. For example, checking both the remaining stock and a returned confirmation object of the same call is still one behaviour. The rule is about one Act, not one Assert line.
The comments // Arrange, // Act and // Assert are optional. In short tests, blank lines between the three blocks are often enough. Keep the comments while a team is learning the pattern, or when a block is not obvious at first glance.
Summary
- Arrange-Act-Assert splits a test into its starting state, its single action and its expected result.
- Arrange only what the scenario needs, and keep the values that drive the result visible in the test.
- A test has exactly one Act. Two actions usually mean two tests.
- Assert on observable, public behaviour, and pass the expected value first to
Assert.Equal. - The next lesson shows how xUnit creates and cleans up the test class around each test.