Lesson 07 of 93 · 6 min read · Updated

Organizing a .NET Test Solution by Scope: Unit and Integration Folders and Conventions

As a test suite grows, its layout starts to matter. A developer looking for the tests of a class, or a pipeline that should run only the fast tests, needs a structure that says what each test is. This lesson shows a small solution organized by test scope: production code in one project, tests split into Unit and Integration folders, and shared build settings that turn any project named *.Tests into an xUnit test project.

Technical Requirements

  1. .NET 11 SDK (projects target net11.0)
  2. xunit 2.9.3, xunit.runner.visualstudio 4.0.1, Microsoft.NET.Test.Sdk 18.12.0
  3. coverlet.collector 10.1.1
  4. Central package management (Directory.Packages.props) and a shared Directory.Build.props

The Layout

M01.Foundations/ production code (class library)
Domain/
Cart.cs
CartLine.cs
DiscountPolicy.cs
Ports/
IExchangeRateProvider.cs
PricingService.cs
Infrastructure/
InMemoryExchangeRateProvider.cs

M01.Foundations.Tests/ main xUnit test project
Unit/
CartTests.cs
DiscountPolicyTests.cs
Integration/
PricingServiceIntegrationTests.cs

M01.Foundations.MSTestExamples/ framework comparison (MSTest)
M01.Foundations.NUnitExamples/ framework comparison (NUnit)
M01.Foundations.XUnitV3Examples/ xUnit v3 / Microsoft.Testing.Platform sample

There is one production project and one main test project. Inside the test project, the first folder level is the scope of the test, not the folder of the class under test. Every test file in Unit/ runs in-process and needs no infrastructure. Every test file in Integration/ connects real components.

The production project is organized by role. Domain holds pure business rules, Ports holds the interfaces that hide boundaries plus the services that use them, and Infrastructure holds the implementations of those boundaries. This split makes the test split almost automatic: classes in Domain get unit tests, and classes in Infrastructure get integration tests.

Folders, Namespaces and Traits Say the Same Thing

The folder name shows up in three places, and they should agree.

M01.Foundations.Tests/Unit/DiscountPolicyTests.cs

using M01.Foundations.Domain;

namespace M01.Foundations.Tests.Unit;

[Trait("Kapsam", "Unit")]
public class DiscountPolicyTests
{
// ...
}

M01.Foundations.Tests/Integration/PricingServiceIntegrationTests.cs

using M01.Foundations.Domain;
using M01.Foundations.Infrastructure;
using M01.Foundations.Ports;

namespace M01.Foundations.Tests.Integration;

[Trait("Kapsam", "Integration")]
public class PricingServiceIntegrationTests
{
// ...
}

The folder tells a human where to look. The namespace (M01.Foundations.Tests.Unit or M01.Foundations.Tests.Integration) groups the tests in Test Explorer. The trait (Kapsam, meaning "scope") lets the runner filter them with dotnet test --filter "Kapsam=Unit". You could also filter by namespace with --filter "FullyQualifiedName~M01.Foundations.Tests.Unit", which gives the same nine tests. But that filter depends on where a class lives, and it silently changes when someone moves a file. The trait states the scope on the class itself, so it is the better contract for a CI pipeline.

Test class names follow the class under test plus a suffix: DiscountPolicyTests, CartTests, PricingServiceIntegrationTests. The IntegrationTests suffix makes the scope visible even in a flat list of failing tests.

A Test Project with Almost Nothing in It

Here is the complete project file of the main test project:

M01.Foundations.Tests/M01.Foundations.Tests.csproj

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

<ItemGroup>
<ProjectReference Include="..\M01.Foundations\M01.Foundations.csproj" />
</ItemGroup>

</Project>

It has no target framework, no package references and no IsTestProject flag. All of that comes from the shared Directory.Build.props file one level up. MSBuild loads this file automatically for every project in that folder and below it.

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>

<!--
Every project whose name ends with ".Tests" is automatically treated as a TEST PROJECT
and gets the shared test packages. We do not repeat them in each csproj.
-->
<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" />
<!-- To collect code coverage data -->
<PackageReference Include="coverlet.collector" />
</ItemGroup>

<ItemGroup Condition="$(MSBuildProjectName.EndsWith('.Tests'))">
<!-- Global using, so we do not write "using Xunit;" in every test file. -->
<Using Include="Xunit" />
</ItemGroup>
</Project>

The naming convention is the contract. Name a project Something.Tests and it becomes an xUnit v2 test project with coverage, a global using Xunit; and the shared target framework. Adding a new test project to the solution then needs a project file with a single project reference. The package versions themselves sit in Directory.Packages.props, so upgrading xUnit is one change for the whole solution.

Common Mistake: A Convention That Catches Too Much

A convention applies to every project that matches it, including the ones you did not have in mind. The MSTest, NUnit and xUnit v3 sample projects must not get xUnit v2 added to them. MSTest and NUnit would end up with two frameworks in one project, and xUnit v3 cannot coexist with v2 at all. That is why those projects are named ...MSTestExamples, ...NUnitExamples and ...XUnitV3Examples and deliberately do not end with .Tests. Each of them sets IsTestProject to true and chooses its own packages:

M01.Foundations.NUnitExamples/M01.Foundations.NUnitExamples.csproj

<!--
NUnit sample.
The project name deliberately does NOT end with ".Tests": Directory.Build.props
adds xUnit v2 automatically to projects ending with ".Tests". Here we pick the framework ourselves.
-->
<PropertyGroup>
<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
</PropertyGroup>

When you rely on a naming convention, write it down in the props file, as the comments above do. A new team member who names a project Billing.Tests and adds NUnit to it will otherwise get confusing build errors and not know where the extra packages came from.

Unit and Integration in Separate Projects?

This solution keeps both scopes in one test project and separates them by folder and trait. For a small codebase that is enough. When integration tests start to need their own packages, such as Testcontainers or a web application factory, or when they need Docker on the build agent, move them to a separate project such as M01.Foundations.Integration.Tests. Keep the .Tests suffix so the shared settings still apply; a name like M01.Foundations.IntegrationTests would not match EndsWith('.Tests'). A separate project can then be built and run only on agents that have the required infrastructure, and the unit test project stays free of heavy dependencies.

Summary

  1. Split the test project by scope first: Unit/ for in-process tests, Integration/ for tests that connect real components.
  2. Keep folders, namespaces and [Trait("Kapsam", ...)] values in agreement; filter CI runs by the trait, because it does not change when a file moves.
  3. A shared Directory.Build.props with a .Tests naming convention keeps each test project file down to a project reference.
  4. Projects that use another framework must not match the convention; name them so they do not end with .Tests.
  5. Move integration tests to their own project once they need their own packages or infrastructure.