Lesson 07 of 93 · 6 min read · Updated
Organizing a .NET Test Solution by Scope: Unit and Integration Folders and Conventions
On this page
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
- .NET 11 SDK (projects target
net11.0) xunit2.9.3,xunit.runner.visualstudio4.0.1,Microsoft.NET.Test.Sdk18.12.0coverlet.collector10.1.1- Central package management (
Directory.Packages.props) and a sharedDirectory.Build.props
The Layout
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
M01.Foundations.Tests/Integration/PricingServiceIntegrationTests.cs
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
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
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
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
- Split the test project by scope first:
Unit/for in-process tests,Integration/for tests that connect real components. - Keep folders, namespaces and
[Trait("Kapsam", ...)]values in agreement; filter CI runs by the trait, because it does not change when a file moves. - A shared
Directory.Build.propswith a.Testsnaming convention keeps each test project file down to a project reference. - Projects that use another framework must not match the convention; name them so they do not end with
.Tests. - Move integration tests to their own project once they need their own packages or infrastructure.