cpnucleo

Testing

Cpnucleo has three test projects covering 25 architecture rules, 49 WebApi unit-test cases in source, and 55 integration tests. The active GitHub Actions gates run architecture tests and container health checks; unit/integration suites are kept for local/manual validation.


Test Projects Overview

ProjectFrameworkFocusKey Libraries
Architecture.TestsxUnitClean Architecture rulesNetArchTest.Rules, FluentAssertions
WebApi.Unit.TestsNUnitEndpoint unit testsFakeItEasy, Shouldly, FastEndpoints
WebApi.Integration.TestsxUnit v355 endpoint integration testsFastEndpoints.Testing, Shouldly

Architecture Tests (test/Architecture.Tests/)

These tests enforce Clean Architecture dependency rules at build time using NetArchTest and FluentAssertions. They run as part of both the PR build check and the release pipeline.

Layer Dependency Tests

TestRule
Domain_Should_Not_HaveDependencyOnOtherProjectsDomain has no dependency on Infrastructure, WebApi, IdentityApi, GrpcServer, GrpcServer.Contracts, or WebClient
Infrastructure_Should_Not_HaveDependencyOnOtherProjectsInfrastructure has no dependency on WebApi, IdentityApi, GrpcServer, GrpcServer.Contracts, or WebClient
Infrastructure_Repositories_Should_HaveDependencyOnDomainRepository implementations in Infrastructure depend on Domain
GrpcServerContracts_Should_OnlyDependOnDomainGrpcServer.Contracts has no dependency on Infrastructure or presentation layers
WebApi_Should_NotDependOnGrpcServerWebApi does not depend on GrpcServer
IdentityApi_Should_NotDependOnGrpcServerIdentityApi does not depend on GrpcServer

Domain Layer Tests

TestRule
Domain_Entities_Should_InheritFromBaseEntityAll non-abstract entities in Domain.Entities inherit from BaseEntity
Domain_Repositories_Should_BeInterfacesAll types starting with “I” in Domain.Repositories are interfaces
Domain_Entities_Should_BeSealedAll non-abstract entities are sealed

Infrastructure Layer Tests

TestRule
Infrastructure_Repositories_Should_ImplementDomainInterfacesRepository classes implement IRepository<>
Infrastructure_DbContext_Should_BeInCorrectNamespaceDbContext classes reside in Infrastructure.Common.Context

Naming Convention Tests

TestRule
WebApi_Dtos_Should_HaveDtoSuffixDTOs in WebApi.Common.Dtos end with “Dto”
GrpcServer_Handlers_Should_HaveHandlerSuffixHandler classes end with “Handler”
GrpcServerContracts_Commands_Should_HaveCommandSuffixCommand classes end with “Command”
GrpcServerContracts_Dtos_Should_HaveDtoSuffixDTOs in GrpcServer.Contracts end with “Dto”
WebApi_Endpoints_Should_BeNamedEndpointAll endpoint classes are named “Endpoint”
IdentityApi_Endpoints_Should_BeNamedEndpointAll IdentityApi endpoint classes are named “Endpoint”

Clean Architecture Pattern Tests

TestRule
Domain_Should_NotDependOnEntityFrameworkDomain has no dependency on Microsoft.EntityFrameworkCore
Domain_Should_NotDependOnDapperDomain has no dependency on Dapper
Domain_Should_NotDependOnNpgsqlDomain has no dependency on Npgsql
Domain_Models_Should_BeRecordsOrClassesModels in Domain.Models are classes or sealed
Domain_Repositories_Should_StartWithIRepository interfaces start with “I”
Infrastructure_Should_NotContainInterfacesOnly IApplicationDbContext is an acceptable public interface in Infrastructure
GrpcServer_Handlers_Should_HaveDependencyOnDomaingRPC handlers depend on the Domain layer

Unit Tests (test/WebApi.Unit.Tests/)

Unit tests for WebApi endpoints using NUnit with FakeItEasy for mocking and Shouldly for assertions. The source contains 49 test cases, but the suite is not part of the active CI gate and currently needs cleanup around several Remove* request-model references before it compiles end to end.

Structure

WebApi.Unit.Tests/
├── Endpoints/
│   └── ...                    # Tests organized by endpoint
├── Usings.cs
└── WebApi.Unit.Tests.csproj

Key Libraries

LibraryPurpose
NUnitTest framework
FakeItEasyMocking framework
ShouldlyAssertion library
FastEndpointsEndpoint testing support
coverlet.collectorCode coverage

Integration Tests (test/WebApi.Integration.Tests/)

55 integration tests exercise the FastEndpoints request pipeline with xUnit v3 and shared host fixtures.

Structure

WebApi.Integration.Tests/
├── AssemblyInfo.cs
├── Endpoints/
│   └── ...                    # Integration tests by endpoint
├── Hosts/
│   └── ...                    # Test host/server configuration
├── Usings.cs
└── WebApi.Integration.Tests.csproj

Key Libraries

LibraryPurpose
xUnit v3Test framework
FastEndpoints.TestingIn-memory test server for FastEndpoints
ShouldlyAssertion library
Microsoft.NET.Test.SdkTest SDK

Prerequisites

Integration tests require a database-backed test environment. For manual runs, start PostgreSQL with Docker Compose first:

docker compose up db -d --build --force-recreate
sleep 30

Integration tests are present in source but are not part of the active GitHub Actions gates; PR and release workflows currently run architecture tests and container health checks.


Running Tests

Run All Tests

dotnet test cpnucleo.slnx

Run Architecture Tests Only

dotnet test test/Architecture.Tests/

Run Unit Tests Only

dotnet test test/WebApi.Unit.Tests/

Run Integration Tests (requires running database)

# Start the database first
docker compose up db -d
sleep 30

# Run integration tests
dotnet test test/WebApi.Integration.Tests/

Run with Code Coverage

dotnet test --collect:"XPlat Code Coverage"

CI Pipeline Test Execution

Architecture tests run automatically in both CI workflows:

  • build-check.yml (PR): runs architecture tests for each service (WebApi, GrpcServer, IdentityApi, WebClient)
  • main-release.yml (push to main): runs architecture tests before building Docker images

Unit and integration test projects remain available for local/manual runs; the active GitHub Actions gates run architecture tests and container health checks. Latest local audit: Architecture.Tests passes 25/25, while WebApi.Unit.Tests has compile drift around removed Remove*.Request model types.