Lesson 08 of 93 · 5 min read · Updated

Creating an xUnit Test Project with Directory.Build.props and Central Package Management

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

  1. .NET 11 SDK
  2. xunit 2.9.3
  3. xunit.runner.visualstudio 4.0.1
  4. Microsoft.NET.Test.Sdk 18.12.0
  5. 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:

M02.XUnitBasics/
M02.XUnitBasics.csproj
Product.cs
Catalog.cs
M02.XUnitBasics.Tests/
M02.XUnitBasics.Tests.csproj
FirstTestTests.cs

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:

dotnet new classlib -n M02.XUnitBasics
dotnet new xunit -n M02.XUnitBasics.Tests
dotnet add M02.XUnitBasics.Tests reference M02.XUnitBasics

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

<Project Sdk="Microsoft.NET.Sdk">

</Project>

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

<Project Sdk="Microsoft.NET.Sdk">

<ItemGroup>
<ProjectReference Include="..\M02.XUnitBasics\M02.XUnitBasics.csproj" />
</ItemGroup>

<ItemGroup>
<!-- For fluent assertion syntax examples. -->
<PackageReference Include="FluentAssertions" />
<!-- The real snapshot library. The hand-made version is in SnapshotTests.cs. -->
<PackageReference Include="Verify.Xunit" />
</ItemGroup>

<ItemGroup>
<!-- Lets us call Verify(...) without a class prefix. -->
<Using Include="VerifyXunit.Verifier" Static="true" />
</ItemGroup>

<ItemGroup>
<!-- Reference files for the snapshot test are copied to the output folder. -->
<None Update="Snapshots\*.verified.txt" CopyToOutputDirectory="PreserveNewest" />
</ItemGroup>

</Project>

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

<Project>
<PropertyGroup>
<TargetFramework>net11.0</TargetFramework>
<LangVersion>latest</LangVersion>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>

<!-- Training project: we want to see warnings, but they should not stop the build. -->
<TreatWarningsAsErrors>false</TreatWarningsAsErrors>

<!-- Central package version management: versions live in Directory.Packages.props. -->
<ManagePackageVersionsCentrally>true</ManagePackageVersionsCentrally>
</PropertyGroup>

<PropertyGroup Condition="$(MSBuildProjectName.EndsWith('.Tests'))">
<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
</PropertyGroup>

<ItemGroup Condition="$(MSBuildProjectName.EndsWith('.Tests'))">
<!-- xUnit: the test framework (DEFINES the tests) -->
<PackageReference Include="xunit" />
<!-- VSTest adapter: the platform side that DISCOVERS and RUNS the tests -->
<PackageReference Include="xunit.runner.visualstudio" />
<PackageReference Include="Microsoft.NET.Test.Sdk" />
<!-- Collects code coverage data -->
<PackageReference Include="coverlet.collector" />
</ItemGroup>

<ItemGroup Condition="$(MSBuildProjectName.EndsWith('.Tests'))">
<!-- Global using, so no test file needs "using Xunit;". -->
<Using Include="Xunit" />
</ItemGroup>
</Project>

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:

PackageRole
xunitThe framework: [Fact], [Theory], Assert
xunit.runner.visualstudioThe adapter that lets dotnet test and IDEs discover and run xUnit tests
Microsoft.NET.Test.SdkThe test host that dotnet test starts
coverlet.collectorCollects 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

<Project>
<PropertyGroup>
<ManagePackageVersionsCentrally>true</ManagePackageVersionsCentrally>
</PropertyGroup>

<ItemGroup Label="Test infrastructure">
<PackageVersion Include="xunit" Version="2.9.3" />
<PackageVersion Include="xunit.runner.visualstudio" Version="4.0.1" />
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="18.12.0" />
<PackageVersion Include="coverlet.collector" Version="10.1.1" />
</ItemGroup>

<ItemGroup Label="Assertion and test helpers">
<!-- FluentAssertions 7.x is Apache-2.0 licensed and free. 8.x moved to a commercial licence. -->
<PackageVersion Include="FluentAssertions" Version="7.2.2" />
</ItemGroup>

<ItemGroup Label="Alternative libraries">
<PackageVersion Include="Verify.Xunit" Version="31.12.5" />
</ItemGroup>
</Project>

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:

dotnet test M02.XUnitBasics.Tests

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

  1. Keep production code and tests in separate projects. Name the test project after the project under test with a .Tests suffix.
  2. Directory.Build.props applies shared settings, such as net11.0 and nullable reference types, to every project below it.
  3. A condition on MSBuildProjectName turns every *.Tests project into a test project with the same four packages.
  4. Central Package Management keeps all package versions in Directory.Packages.props, so versions cannot drift between projects.
  5. dotnet test builds and runs the tests. The next lesson writes the first real test with Arrange-Act-Assert.