Architecture test guidance for .NET using NetArchTest and ArchUnitNET. Use when enforcing architectural boundaries, testing module dependencies, validating layer constraints, or creating performance fitness functions. Includes code generation templates.
This skill is limited to using the following tools:
references/archunitnet-patterns.mdreferences/dependency-rules.mdreferences/netarchtest-patterns.mdreferences/performance-fitness.mdreferences/templates/architecture-test-template.csreferences/templates/performance-test-template.csUse this skill when you need to:
Keywords: fitness functions, architecture tests, NetArchTest, ArchUnitNET, dependency rules, layer constraints, architectural boundaries, module isolation, architecture validation, performance tests
Fitness functions are automated tests that validate architectural characteristics. They provide objective, repeatable verification that the system maintains desired properties as it evolves.
| Type | Validates | Example |
|---|---|---|
| Dependency | Component relationships | "Domain cannot depend on Infrastructure" |
| Layer | Vertical slice rules | "Controllers only call Application layer" |
| Naming | Convention compliance | "Handlers must end with 'Handler'" |
| Performance | Runtime characteristics | "API response < 200ms at p95" |
| Cyclomatic | Code complexity | "No method > 10 cyclomatic complexity" |
# For NetArchTest (simpler, recommended for most cases)
dotnet add package NetArchTest.Rules
# For ArchUnitNET (more powerful, Java-like syntax)
dotnet add package ArchUnitNET
dotnet add package ArchUnitNET.xUnit # or .NUnit
dotnet new xunit -n YourSolution.ArchitectureTests
dotnet add YourSolution.ArchitectureTests reference src/YourSolution.Domain
dotnet add YourSolution.ArchitectureTests reference src/YourSolution.Application
dotnet add YourSolution.ArchitectureTests reference src/YourSolution.Infrastructure
public class DependencyTests
{
[Fact]
public void Domain_ShouldNotDependOn_Infrastructure()
{
var result = Types.InAssembly(typeof(Order).Assembly)
.ShouldNot()
.HaveDependencyOn("YourSolution.Infrastructure")
.GetResult();
Assert.True(result.IsSuccessful, result.FailingTypeNames?.FirstOrDefault());
}
}
NetArchTest provides a fluent API for testing architectural constraints.
Detailed patterns: See references/netarchtest-patterns.md
// Dependency constraints
Types.InAssembly(domainAssembly)
.ShouldNot()
.HaveDependencyOn("Microsoft.EntityFrameworkCore");
// Naming conventions
Types.InAssembly(applicationAssembly)
.That()
.ImplementInterface(typeof(IRequestHandler<,>))
.Should()
.HaveNameEndingWith("Handler");
// Layer isolation
Types.InNamespace("Domain")
.ShouldNot()
.HaveDependencyOnAny("Application", "Infrastructure", "Api");
ArchUnitNET offers more expressive rules with a syntax similar to ArchUnit for Java.
Detailed patterns: See references/archunitnet-patterns.md
// Define architecture layers
private static readonly Architecture Architecture =
new ArchLoader().LoadAssemblies(
typeof(Order).Assembly,
typeof(OrderHandler).Assembly,
typeof(OrderRepository).Assembly
).Build();
private static readonly IObjectProvider<IType> DomainLayer =
Types().That().ResideInNamespace("Domain").As("Domain Layer");
private static readonly IObjectProvider<IType> InfrastructureLayer =
Types().That().ResideInNamespace("Infrastructure").As("Infrastructure Layer");
[Fact]
public void DomainLayer_ShouldNotDependOn_InfrastructureLayer()
{
IArchRule rule = Types().That().Are(DomainLayer)
.Should().NotDependOnAny(InfrastructureLayer);
rule.Check(Architecture);
}
Common dependency rules for modular monoliths:
Full catalog: See references/dependency-rules.md
[Fact]
public void Modules_ShouldNotCrossReference_CoreProjects()
{
var orderingCore = Types.InAssembly(typeof(Order).Assembly);
var inventoryCore = Types.InAssembly(typeof(Product).Assembly);
// Ordering.Core cannot reference Inventory.Core
var result = orderingCore
.ShouldNot()
.HaveDependencyOn("Inventory.Core")
.GetResult();
Assert.True(result.IsSuccessful);
}
[Fact]
public void SharedKernel_ShouldNotDependOn_AnyModule()
{
var sharedKernel = Types.InAssembly(typeof(Entity).Assembly);
var result = sharedKernel
.ShouldNot()
.HaveDependencyOnAny(
"Ordering", "Inventory", "Shipping", "Customers")
.GetResult();
Assert.True(result.IsSuccessful);
}
Test runtime characteristics to ensure performance standards.
Detailed guide: See references/performance-fitness.md
[Fact]
public async Task Api_ShouldRespondWithin_200ms()
{
var client = _factory.CreateClient();
var stopwatch = Stopwatch.StartNew();
var response = await client.GetAsync("/api/orders/123");
stopwatch.Stop();
Assert.True(stopwatch.ElapsedMilliseconds < 200,
$"Response took {stopwatch.ElapsedMilliseconds}ms");
}
[Fact]
public void Handler_ShouldNotAllocateExcessiveMemory()
{
var before = GC.GetTotalMemory(true);
for (int i = 0; i < 1000; i++)
{
_handler.Handle(new GetOrderQuery(Guid.NewGuid()));
}
var after = GC.GetTotalMemory(true);
var allocated = (after - before) / 1000; // Per operation
Assert.True(allocated < 10_000, $"Allocated {allocated} bytes per operation");
}
Use these templates to quickly create architecture tests:
references/templates/architecture-test-template.cs - Full test class scaffoldreferences/templates/performance-test-template.cs - Performance test patterns# Copy template and customize
cp templates/architecture-test-template.cs tests/ArchitectureTests.cs
- name: Run Architecture Tests
run: dotnet test --filter Category=Architecture
continue-on-error: false # Fail pipeline on violations
[Trait("Category", "Architecture")]
public class DependencyTests
{
// Architecture tests run separately from unit tests
}
Fitness functions enforce the boundaries discovered through event storming:
Event Storming → Bounded Contexts
↓
Modular Architecture → Module Structure
↓
Fitness Functions → Enforce Boundaries
After event storming identifies bounded contexts:
Test finds no types:
False positives:
Performance tests flaky:
references/netarchtest-patterns.md - NetArchTest usage patternsreferences/archunitnet-patterns.md - ArchUnitNET usage patternsreferences/performance-fitness.md - Performance testing patternsreferences/dependency-rules.md - Common dependency rules catalogreferences/templates/architecture-test-template.cs - Test class templatereferences/templates/performance-test-template.cs - Performance test templateDate: 2025-12-22 Model: claude-opus-4-5-20251101