Lesson 66 of 93 · 5 min read · Updated
Organizing Tests by Layer and Scope in a Layered .NET Solution
On this page
A test suite that grows without structure becomes hard to read and slow to run. Someone looking for the test of a business rule should not have to search through tests that build a DI container. This lesson shows how to organize the tests of a layered application into folders that mirror the production layers, and why each folder has its own scope, its own dependencies and its own speed.
Technical Requirements
- .NET 11 SDK
- xUnit 2.9.3
- FluentAssertions 7.2.2
- Moq 4.21.0
- Microsoft.Extensions.TimeProvider.Testing 11.0.0
- Microsoft.Extensions.DependencyInjection 11.0.0
- NetArchTest.Rules 1.3.2
One Folder per Layer, One File per Concern
The production project has Domain, Application and Infrastructure folders. The test project mirrors the first two and adds a folder for tests that look at the whole assembly:
Inside each folder, files are grouped by concern, not by class. EntityAndAggregateTests covers the Order entity and its aggregate rules. DomainEventTests covers the events that the same class raises. Two files test one class because they answer two different questions.
The test project references the production project and only the packages that some test in it needs:
M08.LayeredDdd.Tests/M08.LayeredDdd.Tests.csproj
Versions live in a central Directory.Packages.props, and xUnit with the test SDK comes from a shared Directory.Build.props, so the project file stays short.
Domain Tests: Plain C#
Tests in the Domain folder use nothing but the production types and FluentAssertions. There is no mock, no container and no clock abstraction. The time is a constant.
M08.LayeredDdd.Tests/Domain/EntityAndAggregateTests.cs
If a test in this folder needs a mock, treat it as a signal. Either the test is in the wrong folder, or the domain model has picked up a dependency it should not have.
Application Tests: Ports Replaced, Domain Real
Tests in the Application folder exercise use cases. The use case talks to ports, so the test supplies in-memory adapters, a Moq mock for the port it wants to control, and a FakeTimeProvider for the clock. The Order aggregate stays real.
M08.LayeredDdd.Tests/Application/PlaceOrderHandlerTests.cs
The same folder also holds two wider kinds of tests. CommandPipelineIntegrationTests builds a real ServiceCollection to check registrations and pipeline order. SubcutaneousTests runs a full use case through the real layers. They stay in Application because they test the application layer, but their file names make their wider scope obvious.
Architecture Tests: The Whole Assembly
The Architecture folder holds tests that do not exercise behavior at all. They load the production assembly and check rules about it.
M08.LayeredDdd.Tests/Architecture/ArchitectureTests.cs
Scope at a Glance
Putting it together, each folder answers a different question and pays a different cost:
| Folder | Question | Dependencies | Speed |
|---|---|---|---|
Domain/ | Is the business decision correct? | Production types only | Fastest |
Application/ (handler, behavior) | Does the use case orchestrate correctly? | In-memory adapters, mocks, fake clock | Fast |
Application/ (integration, subcutaneous) | Do the real pieces work together? | Real ServiceCollection, real layers | Still in-process, slightly slower |
Architecture/ | Are the layer rules respected? | NetArchTest, reflection | Fast |
This structure pays off in three ways. When a test fails, its folder already tells you which layer is broken. When you need a quick feedback loop, you can run only the domain tests with a filter such as dotnet test --filter "FullyQualifiedName~M08.LayeredDdd.Tests.Domain". And when a new team member asks where the test for a rule should go, the answer is the folder of the layer that owns the rule.
A common mistake is to organize tests only by production class name, one test file per class. That pushes very different tests into the same file, for example identity checks next to event checks, and it hides the scope of each test. Organize by layer first, then by concern.
Summary
- Mirror the production layers in the test project:
Domain/,Application/and a separateArchitecture/folder. - Domain tests use only production types; a mock there is a design smell.
- Application tests replace ports with in-memory adapters or mocks and keep the domain model real.
- Wider tests (container integration, subcutaneous) live with the layer they test, with names that show their scope.
- Group files by concern, not one file per class, so each file answers one question.