Lesson 08 of 93 · 5 min read · Updated
Creating an xUnit Test Project with Directory.Build.props and Central Package Management
On this page
Before you write a single assertion, you need a test project that builds, finds its tests and runs them. This lesson sets up a production class library and an xUnit test project next to it, and moves the shared build settings into one Directory.Build.props file so every new test project gets the same setup for free. A clean project layout matters because test infrastructure that is copied by hand drifts apart quickly.
Technical Requirements
- .NET 11 SDK
- xunit 2.9.3
- xunit.runner.visualstudio 4.0.1
- Microsoft.NET.Test.Sdk 18.12.0
- coverlet.collector 10.1.1
Two Projects: Production Code and Tests
A test project is a normal .NET project that references the code it tests. Keep them separate. The production project must not know that tests exist, and the test project should not ship with your application.
The layout used in this lesson looks like this:
The naming convention is simple: the test project has the same name as the project under test, plus .Tests. This makes it easy to find the tests for any class, and as you will see below, the build uses that suffix to decide which projects are test projects.
You can create both projects from the command line:
The xunit template adds package references with versions to the new .csproj. In this setup the versions live in one central file instead, so you can delete those generated references and keep only what is specific to the project.
The Project Files
Because the shared settings come from a parent folder, both project files stay very short. The production project is empty:
M02.XUnitBasics/M02.XUnitBasics.csproj
The test project only declares what is unique to it: the reference to the production project, two extra packages for later lessons, a global static using, and a rule that copies snapshot files to the output folder.
M02.XUnitBasics.Tests/M02.XUnitBasics.Tests.csproj
Notice that PackageReference has no Version attribute. That is Central Package Management at work, which the next section explains.
Shared Settings in Directory.Build.props
MSBuild automatically imports a file named Directory.Build.props from the project folder or any parent folder. Put it in the folder that contains all your projects, and every .csproj below it picks up the same settings. You set the target framework, nullable reference types and implicit usings once.
Directory.Build.props
The key idea is the condition $(MSBuildProjectName.EndsWith('.Tests')). Any project whose name ends with .Tests becomes a test project automatically and receives the four test packages. A new test project needs no copy-pasted boilerplate. Each package has a clear job:
| Package | Role |
|---|---|
xunit | The framework: [Fact], [Theory], Assert |
xunit.runner.visualstudio | The adapter that lets dotnet test and IDEs discover and run xUnit tests |
Microsoft.NET.Test.Sdk | The test host that dotnet test starts |
coverlet.collector | Collects code coverage when you ask for it |
The global Using Include="Xunit" is why the test files in this series never start with using Xunit;.
Package Versions in One Place
With ManagePackageVersionsCentrally turned on, every version lives in Directory.Packages.props. Project files name the package, and this file decides the version. Twenty test projects can never end up on twenty different xUnit versions.
Directory.Packages.props
The real file contains more packages for later topics. Only the ones this project uses are shown. Note the comment on FluentAssertions: version 8.x is sold under a commercial licence, so this project pins the free 7.x line on purpose. When you upgrade a package, check its licence as well as its changelog. A common mistake is to add a Version attribute back into a .csproj after turning on central management. NuGet reports an error for that, which is good: it stops the drift before it starts.
Running the Tests
Point dotnet test at the test project folder. It restores packages, builds both projects and runs every test it discovers:
If the command reports zero tests, check two things first. The project name must end with .Tests so the shared packages are added, and the test methods must be public and marked with [Fact] or [Theory].
Summary
- Keep production code and tests in separate projects. Name the test project after the project under test with a
.Testssuffix. Directory.Build.propsapplies shared settings, such asnet11.0and nullable reference types, to every project below it.- A condition on
MSBuildProjectNameturns every*.Testsproject into a test project with the same four packages. - Central Package Management keeps all package versions in
Directory.Packages.props, so versions cannot drift between projects. dotnet testbuilds and runs the tests. The next lesson writes the first real test with Arrange-Act-Assert.