Lesson 01 of 93 · 7 min read · Updated
What Is a Unit Test? Fast, Deterministic Tests for Pure Business Rules in xUnit
On this page
Everyone on a .NET team says "unit test", but people often mean different things by it. This lesson gives the term a working definition and then applies it to a small discount rule and its xUnit tests. Once you know what makes a test a unit test, you can tell why some tests run in milliseconds and never flake, while others are slow and break for reasons that have nothing to do with your code.
Technical Requirements
- .NET 11 SDK (projects target
net11.0) xunit2.9.3xunit.runner.visualstudio4.0.1 andMicrosoft.NET.Test.Sdk18.12.0coverlet.collector10.1.1 (optional, for coverage data)
The sample has one class library, M01.Foundations, and one xUnit test project, M01.Foundations.Tests. Run the tests with dotnet test M01.Foundations.Tests.
A Working Definition
A unit test has four properties. Use them as a checklist when you write or review a test:
- It verifies one observable behavior. That is not the same as one method. A behavior can involve several methods or classes.
- It is deterministic. The same input always gives the same result, on every machine and on every run.
- It does not depend on the network, a database, the clock or shared state. Nothing outside the process takes part.
- It returns a result in milliseconds. You can run hundreds of these tests after every small change.
The last property follows from the first three. A test is fast because it touches nothing slow. So the easiest code to unit test is code that has no infrastructure to set up in the first place.
The Code Under Test: A Pure Business Rule
The DiscountPolicy class below is a pure function. It reads no database, calls no HTTP service, does not ask for the current time and uses no random numbers. Its result depends only on its argument.
M01.Foundations/Domain/DiscountPolicy.cs
Two design choices make this class easy to test. First, the invalid input throws an exception instead of returning a quiet default. A thrown exception is something a test can see and assert on. Second, the threshold and the rate are public constants, so a test can refer to DiscountPolicy.Threshold instead of repeating the magic number 1000.
The Unit Tests
Each test below follows the Arrange-Act-Assert pattern. Look at the Arrange step in every test: none of them builds a database, a mock server or a configuration object. That is where the fast feedback comes from.
M01.Foundations.Tests/Unit/DiscountPolicyTests.cs
The test names follow the Method_Condition_ExpectedResult pattern. When a test fails in CI, the name alone tells you which rule broke. You do not need to open the file to understand the failure.
The [Trait("Kapsam", "Unit")] attribute marks the class as a unit test class. The key Kapsam means "scope". A later lesson on the test pyramid uses this trait to run unit tests and integration tests separately.
Common Mistake: Skipping the Boundary Test
Why does CalculateDiscount(1000) get a separate test when 2500 is already covered? Because the rule says "1000 or more", and the code expresses that with >=. If someone changes it to >, the 2500 test still passes and the 999.99 test still passes. Only the test at exactly 1000 fails. Confusing > with >= is one of the most frequent bugs in business rules, so always test the exact boundary value and the values right next to it.
This idea also connects to mutation testing. A mutation testing tool changes >= to > on purpose and checks whether any test fails. A missing boundary test shows up as a surviving mutant.
Small Scope Does Not Mean One Method
A unit test can exercise more than one class. The test below uses Cart, CartLine and DiscountPolicy together. It is still a unit test, because it never crosses an out-of-process boundary such as a database or an HTTP call.
M01.Foundations.Tests/Unit/CartTests.cs
If you define "unit" as "one class", you end up mocking every collaborator, even plain in-memory objects. Those tests are tied to the internal structure and break on every refactoring. Define the unit as a behavior instead, and isolate only the things that are slow or non-deterministic.
Determinism, Shown Directly
The test project also contains a test that calls the method 1000 times and checks that only one distinct result comes back:
M01.Foundations.Tests/Unit/DiscountPolicyTests.cs
Would you write this test in a real project? No. A pure function is deterministic by construction, so this test can never fail and adds no protection. It only makes the concept visible. Tests that cannot fail are a form of trivial test, and they cost maintenance time without catching bugs.
Summary
- A unit test checks one observable behavior, is deterministic, uses no out-of-process resources and runs in milliseconds.
- Pure functions such as
DiscountPolicy.CalculateDiscountare the easiest code to unit test, because the Arrange step needs no infrastructure. - Test exact boundary values: a
>versus>=mistake passes every test except the one at the boundary. - The error path is a behavior; assert on the exception type and, when callers rely on it, on
ParamName. - A unit can span several in-process classes; the next lesson shows what changes when a test crosses a real boundary.