Rules for Unit and Integration Tests
This rule defines the best practices and tools to use for writing unit and integration tests in the backend project.
Libraries and Tools
- Test Framework: Always use
xunit.v3for unit and integration tests. - Mocks: Use
FakeItEasyto create mocks in unit tests. - Contract Tests or Advanced Mocks: Use
TestcontainersandMicrocksto simulate SOAP or REST calls, or for contract tests. - Integration Tests: Configure
Testcontainersfor databases or other external dependencies.
General Steps
-
Unit Tests:
- Write tests for each valid and invalid scenario.
- Use
FakeItEasyto mock dependencies. - Verify exceptions and expected results.
-
Integration Tests:
- Set up a test environment with
Testcontainers. - Simulate SOAP or REST calls with
Microcksif necessary. - Validate the entire business flow.
- Set up a test environment with
-
Performance Tests:
- Add tests to validate the performance of critical features.
Unit Tests
- Located in
tests/[project].UnitTests/. - Test only the Domain and Application layers.
- Use
FakeItEasyto mock dependencies. - Do not interact with real databases or external services.
- Focus on business logic, use cases, and validation.
- Example folders:
UseCases/,Services/.
Integration Tests
- Located in
tests/[project].IntegrationTests/. - Test the Infrastructure and Api layers, and the integration between layers.
- Use
Testcontainersto set up real or simulated external dependencies (databases, APIs, etc.). - Use
Microcksfor contract or advanced integration tests (SOAP, REST, events). - Validate the entire business flow, including data persistence and external calls.
- Example folders:
Features/(for API endpoint tests).
Best Practices
- Always write tests before implementation (TDD).
- Document test cases in the corresponding files.
- Use explicit test names to describe their purpose.
Additional Rule: Continuous Test Execution
- After every significant change or addition, run
dotnet testordotnet watchto verify the current state of the project. - This ensures that all tests pass and helps identify issues early in the development process.
- Document the results of the test runs in the corresponding task or user story.
Updates
This rule must be updated if new tools or practices are adopted in the backend project.