This document provides instructions for running the unit tests, generating test coverage reports, and interpreting the results for the Task Management System.
- Prerequisites
- Running Unit Tests
- Running Specific Test Suites
- Generating Test Coverage Report
- Viewing the Coverage Report
- Interpreting Test Coverage Metrics
- Continuous Integration (CI) Testing
- Troubleshooting
- Example Commands
- External Resources
- Ensure you have Go installed (version 1.20 or later).
- Install the required dependencies by running:
go mod tidy
To run all unit tests in the project, execute the following command in the root directory of the project:
go test -v ./...- The
-vflag enables verbose output, showing detailed information about each test. - The
./...ensures that tests in all subdirectories are executed.
To run tests for a specific package, navigate to the package directory or specify the package path. For example:
-
To run tests for the
Usecasespackage:go test -v ./Usecases -
To run tests for the
Infrastructurepackage:go test -v ./Infrastructure
To generate a test coverage report, use the following command:
go test -coverprofile=coverage.out ./...This will create a file named coverage.out in the root directory.
-
To view the coverage summary in the terminal:
go tool cover -func=coverage.out
-
To view the coverage report in a browser:
go tool cover -html=coverage.out
This will open an interactive HTML report showing which lines of code are covered by tests.
- Coverage Percentage: Indicates the percentage of code covered by tests. Aim for a high percentage (e.g., 80% or above) to ensure good test coverage.
- Uncovered Lines: The HTML report highlights uncovered lines in red. These lines should be reviewed to determine if additional tests are needed.
The project includes a GitHub Actions CI pipeline that automatically runs tests and uploads the coverage report. The CI configuration is located in .github/workflows/ci.yml.
- Run Tests: Executes all unit tests using
go test. - Generate Coverage Report: Creates a
coverage.outfile. - Upload Coverage Report: Saves the coverage report as an artifact for review.
- Test Failures: If a test fails, review the error message and the corresponding test case to identify the issue.
- Dependency Issues: Run
go mod tidyto ensure all dependencies are installed. - Coverage Report Issues: Ensure the
coverage.outfile is generated before runninggo tool cover.
-
Run all tests:
go test -v ./... -
Generate and view coverage report:
go test -coverprofile=coverage.out ./... go tool cover -html=coverage.out -
Run tests for a specific file:
go test -v ./Usecases/task_usecases_test.go