Testing
This is the map of the test projects: what each one proves about which layer, what it needs to run, and
when a test of yours belongs in it. The README, .claude/rules/tests.md (the conventions inside each
project) and the add-feature skill point here rather than repeat it.
| Project | Proves | Needs | Write a test here when |
|---|---|---|---|
Domain. |
the Domain and the SharedKernel in memory: every invariant and transition of an aggregate, the events it raises, value objects, specifications as predicates, the Result kernel |
nothing | you add or change a rule of an aggregate or a value object |
Application. |
each handler's orchestration over faked ports (NSubstitute, FakeTimeProvider), the validators, event handlers, and the decorator pipeline itself (order, span, outcome line, validation) |
nothing | you write a handler, a validator or an event handler, or touch a decorator: one folder per use case under Features/, the validators' bounds in Features/ |
ArchitectureTests |
the rules files hold: layer boundaries and every project under src/ anchored to one, conventions by reflection and source scan (aggregates created through Result, no throw in the Domain, the use cases or the endpoints, time from TimeProvider and ids from their type, no sleep in a test, nothing silencing RSLT001), the error-code vocabulary, options validated at boot, telemetry names and the telemetry reference, every package and container image within the license ceilings; with the UI, the design system (one token file writing every colour as a light/dark pair, contrast on every ground, every class documented, and the JSON export scripts/ writes for a design tool true to the token file and the doc) and the UI rules a scan can see (no endpoint [Authorize], no raw HttpClient, no inline script) |
nothing | you introduce a convention: it gets a rule and a positive control, not just prose |
IntegrationTests |
Persistence against a real PostgreSQL: mappings and round trips, specifications translated to SQL, interceptors, concurrency, the outbox and the idempotency store, retention, the migration matching the model | Docker (Testcontainers starts PostgreSQL) | a query, a mapping, an interceptor or anything transactional changes |
ApiTests |
the HTTP contract through the real pipeline: authentication and each endpoint's permission, problem details, idempotency, caching and conditional requests, the served OpenAPI document equal to the committed one, the reference integration over a scripted socket, the e-mail adapters (the SMTP one against a fake relay on a loopback socket) and, from the AppHost's app model without starting it, where aspire run sends the API's e-mail |
Docker for the in-process tests (contract and serialization tests run without it). The Aspire E2E also needs RUN_ASPIRE_E2E=1 and a local SDK or CI, otherwise it skips; CI runs it as its own step after the other suites, because its processes and Testcontainers would otherwise race for the same host ports; it drops the AppHost's postgres data volume and persistent lifetime (appHost., which an Aspire test of your own calls too), so it runs its own container and database and never touches your aspire run ones |
an endpoint answers something the fitness suites do not already pin: its own 404, a business 409, the field codes of its 400, a notable flow end to end |
tests/TestSupport/ is not a project: it holds the test files several projects share
(ResultAssertions.cs with ShouldBeSuccess(), Invoices/InvoiceFactory.cs), each compiled into the
projects that need it by a linked <Compile Include> in their csproj.
Where to invest: most of a feature's tests belong in Domain.UnitTests and IntegrationTests, because a
rule is cheapest to pin where it lives and a query or a transaction only proves itself against
PostgreSQL. Every endpoint's permission, problem shape, idempotency and OpenAPI entry is already enforced
by the fitness suites, so an ApiTests test is for what they cannot see, and the Aspire E2E and browser
tests stay few.
Running part of the suite
Section titled “Running part of the suite”The test projects run on Microsoft.Testing.Platform (xUnit v3). Narrow a run to one project, then to a class or a method by pattern:
./scripts/dotnet.sh test tests/Trellis.Domain.UnitTests # one project./scripts/dotnet.sh test tests/Trellis.Application.UnitTests --filter-class '*CommandHandlerTests'./scripts/dotnet.sh test tests/Trellis.ArchitectureTests --filter-method '*telemetry_reference*'./scripts/dotnet.sh test tests/Trellis.ApiTests --filter 'FullyQualifiedName~Idempotency' # the VSTest-style expression works tooFilter a project, not Trellis.slnx: every other project then runs zero tests and exits 8, so the run is
red although your test passed (add --ignore-exit-code 8 if you must filter the solution). VSTest-only
switches such as --nologo are not understood either, and a run that reports zero tests is a failure to
look at, never a pass.
Which helper for which goal
Section titled “Which helper for which goal”| Goal | Use | Worked example |
|---|---|---|
| A caller holding exactly some permissions | ApiFactory.: a fresh user each call, minted by the host's ITokenProvider |
EndpointMetadataTests |
| The seeded admin, or a member who can only read | ApiFactory., CreateMemberClientAsync |
ProblemDetailsContractTests |
| Many users behind one address (a proxy, the web host) | ApiFactory. |
AuthRateLimitTests |
| A commit that conflicts or throws, a cache that fails | ApiFactory., then CreateAdminClientOnAsync(host, …) |
CommitFailureTests |
| A valid command or aggregate in an arrange step | InvoiceFactory., InvoiceFactory., InvoiceFactory. (using Trellis.) |
IssueInvoiceCommandHandlerTests, InvoiceCommandValidatorTests |
| An invoice in a given state against PostgreSQL | await Services., IssuedInvoiceAsync(), PayInvoiceAsync(id) (InvoiceDriver; on a fresh scope's ServiceProvider for a separate unit of work) |
OutboxTests |
| An invoice over HTTP in an arrange step | await client., AddInvoiceLineAsync(id), IssueInvoiceAsync(id, dueDate) (InvoiceHttpDriver) |
ProblemDetailsContractTests |
| A POST the drivers do not cover (a test about the create's own answer) | PostWithIdempotencyKeyAsync |
InvoiceContractTests |
| Acting as another user against PostgreSQL | IntegrationTestBase. (TestCurrentUser): set its UserId (null and a UserName for a caller without an id, like the migration job) |
AuditingInterceptorTests |
A Result that must have succeeded |
ShouldBeSuccess() (tests/), which returns a Result<T>'s value and on failure names the error |
InvoiceTests (the message: ResultAssertionsTests) |
| A problem response, header and body | ShouldBeProblemJson(), then read ProblemResponse |
ProblemDetailsContractTests |
A new status or problem code |
a row in ProblemDetailsContractTests |
— |
| Time that passes without sleeping | IntegrationTestBase., ApiFactory. (Reset() it in a finally), FakeTimeProvider |
DataRetentionJanitorTests |
| A limit only Kestrel enforces | RequestLimitTests. |
EndpointMetadataTests. |
| Observers instead of mocks: a clock, a hasher, captured logs | AuthFlowTests. |
AuthFlowTests |
| The shipped defaults, not the fixture's settings | a plain WebApplicationFactory<Program> |
AuthFlowTests. |
| The outcome line and the span of a use case | PipelineHarness and its Logs |
UseCaseOutcomeTests |
| Metrics and spans against PostgreSQL | MetricRecorder, ActivityRecorder, IntegrationTestBase. |
OutboxRetryTests |
| A wire change | UPDATE_OPENAPI_SNAPSHOT=1 dotnet test tests/, then commit docs/ |
OpenApiSnapshotTests |
The documentation of Trellis.Templates 0.2.0 · release notes