csharp-architecture-testing
Comprehensive guidance for writing C# architecture tests using NetArchTest.Rules, TUnit, and TUnit.Assertions.Should to enforce layer dependencies, namespace rules, and naming conventions
- License 许可
- GPL-3.0
- 最近更新
- 2026-06-06
- Stars
- ★ 0
- Forks
- 0
01 前言 Front Matter
- name
- csharp-architecture-testing
- description
- Comprehensive guidance for writing C# architecture tests using NetArchTest.Rules, TUnit, and TUnit.Assertions.Should to enforce layer dependencies, namespace rules, and naming conventions
02 资源文件 7
- 参考文档templates/arch-assert-result.md1.6 KB
- 参考文档templates/arch-complete-example.md4.2 KB
- 参考文档templates/arch-inheritance-rule.md1.3 KB
- 参考文档templates/arch-layer-dependency.md1.1 KB
- 参考文档templates/arch-load-assembly.md844 B
- 参考文档templates/arch-namespace-residency.md1.1 KB
- 参考文档templates/arch-naming-convention.md1.1 KB
03 SKILL.md 正文
C# Architecture Testing
You are the C# Architecture Testing skill for the HypermediaEngine project.
This skill provides comprehensive guidance for writing architecture tests that enforce structural rules — layer isolation, namespace conventions, naming patterns, and interface contracts — using the NetArchTest.Rules fluent API within the TUnit testing framework.
Phase 0 — Context Load (silent)
- Read
.claude/CLAUDE.mdand.claude/rules/testing.mdto understand project conventions and quality gates - Invoke
Skill("manage-memory", args: "sqa-engineer")to load the SQA engineer's persistent memory - Read relevant architecture documentation or existing ArchitectureTests.cs files to understand current architectural constraints
Phase 1 — Architecture Testing Stack
Package: NetArchTest.Rules (NuGet)
Provides a fluent API — Types.InAssembly(...).That()...Should()... — for asserting structural rules over .NET assemblies.
Test Framework: TUnit
Same framework as all other tests in this project. Use [TestClass] and [Test] attributes.
Assertions: TUnit.Assertions.Should
Use ArchRuleResult.IsSuccessful + TUnit.Assertions.Should to assert the result. On failure, include FailingTypeNames in the assertion message.
NuGet: NetArchTest.Rules (latest stable). No other special packages needed.
Phase 2 — Core Patterns
Each pattern is documented in a separate template file. Review the pattern that matches your architectural rule:
1. Loading an Assembly — arch-load-assembly
Use Types.InAssembly(typeof(SomeTypeFromAssembly).Assembly) to target an assembly.
For solution-wide rules, load multiple assemblies and chain .And().
2. Layer Dependency Rule — arch-layer-dependency
Example: "Domain layer must not depend on Infrastructure layer."
Pattern: .That().ResideInNamespace("HypermediaEngine.Domain").ShouldNot().HaveDependencyOn("HypermediaEngine.Infrastructure")
3. Namespace Residency Rule — arch-namespace-residency
Example: "All middleware classes must reside in the Middlewares namespace."
Pattern: .That().HaveNameEndingWith("Middleware").Should().ResideInNamespace("HypermediaEngine.Middlewares")
4. Naming Convention Rule — arch-naming-convention
Example: "All interface types must start with 'I'."
Pattern: .That().AreInterfaces().Should().HaveNameStartingWith("I")
5. Inheritance / Interface Implementation Rule — arch-inheritance-rule
Example: "All request handlers must implement IRequestHandler."
Pattern: .That().ResideInNamespace("...Handlers").Should().ImplementInterface(typeof(IRequestHandler<,>))
6. Asserting Results — arch-assert-result
Always capture TestResult result = rule.GetResult();
Assert result.IsSuccessful with a message that includes string.Join(", ", result.FailingTypes.Select(t => t.FullName ?? t.Name)) so failures are visible.
Use Assert.Multiple() to collect all architecture rule failures in one test run.
Phase 3 — Complete Example
See arch-complete-example for a complete test class enforcing HypermediaEngine architecture rules. It demonstrates:
- A
[TestClass]with 4 test methods, one per rule category - Loading the HypermediaEngine assembly via
typeof(SomeKnownType).Assembly - At least one dependency rule, one namespace rule, one naming rule, and one multi-rule assertion
- Using
Assert.Multiple()to collect failures - Using TUnit.Assertions.Should for the final assertion
Phase 4 — Key Takeaways
- Use
NetArchTest.Rules— fluent API, no extra dependencies. - Load assemblies via
typeof(KnownType).Assembly— never hardcode paths. - Always call
.GetResult()and checkIsSuccessful— includeFailingTypeNamesin the failure message for debuggability. - Wrap all assertions in
Assert.Multiple()— see all violations at once instead of stopping at the first failure. - One test per architectural rule — keeps failures targeted and understandable.
- Architecture tests belong in the existing test project — no separate project needed.
- These tests run with
dotnet testlike any other test — they are part of your regular CI/CD quality gates.
When to Use This Skill
Invoke this skill whenever you are writing tests that enforce structural rules rather than behavior:
- Layer isolation (domain should not reference infrastructure)
- Namespace conventions (all handlers must live in a specific namespace)
- Naming patterns (all interfaces must start with 'I', all exceptions must end with 'Exception')
- Interface contracts (all request handlers must implement a specific interface)
- Dependency direction (presentation layer should not depend on persistence)
These tests run as part of dotnet test and are enforced just like unit and integration tests.
04 同仓库更多 Skills
write-tests
Test implementation skill for HypermediaEngine. Takes a confirmed test plan from design-test-cases and produces compilable, runnable xUnit tests following project conventions. Validates coverage quality through mutation testing. Invoked by the sqa-engineer agent after the test plan is approved.
write-documentation
Structured documentation writing workflow for HypermediaEngine. Writes or updates README.md files at the repo root and in each project directory (src/, tests/, samples/, etc.) based on current implementation, ADRs, and architecture decisions.
write-adr
Writes a numbered Architecture Decision Record (ADR) for a significant architectural choice in the HypermediaEngine project. Invoked by the software-architect agent whenever a consequential design decision is made or ratified. Saves the ADR to docs/architecture/decisions/.
tunit-playwright-ui-testing
Comprehensive guidance for writing coded end-to-end UI tests in C# using TUnit.Playwright. Tests inherit from PageTest — the base class manages all browser lifecycle. Covers NuGet setup, PageTest inheritance, Page Object Model, browser configuration, parallelism limiting, and CI integration. Based on the official TUnit docs at https://tunit.dev/docs/examples/playwright.
triage
Task classification, decomposition, dependency mapping, and routing skill. Invoked by the triage-agent to break any user request into discrete, independently-routable work items with assigned priorities and agent chains.
system-design
Expert system design skill. Use PROACTIVELY to design systems, review architectures, and assist software engineers in learning design principles and implementing them in the code with design patterns.