2025-03-15 19:36:11 +02:00
|
|
|
# Writing & Maintaining Tests
|
|
|
|
|
|
|
|
Pilgrim's tests are written using the testify framework and mockery.
|
|
|
|
|
2025-03-18 22:36:52 +02:00
|
|
|
### Generate Mocks
|
2025-03-15 19:36:11 +02:00
|
|
|
|
|
|
|
The below command, when executed from the project root, will generate mocks for
|
|
|
|
all interfaces in all packages of the project, and put them in the `mocks`
|
|
|
|
directory, under the `pilgrim_mock` package
|
|
|
|
|
|
|
|
```bash
|
2025-03-18 21:45:07 +02:00
|
|
|
$ mockery --all --outpkg pilgrim_mock --output ./internal/mocks
|
2025-03-15 19:36:11 +02:00
|
|
|
```
|
|
|
|
|
2025-03-18 22:36:52 +02:00
|
|
|
### Run Tests
|
2025-03-15 19:36:11 +02:00
|
|
|
|
|
|
|
To run all tests, execute the following from the root directory
|
|
|
|
|
|
|
|
```bash
|
2025-03-18 21:45:07 +02:00
|
|
|
$ go test ./...
|
|
|
|
```
|
|
|
|
|
2025-03-18 22:36:52 +02:00
|
|
|
### Coverage
|
2025-03-18 21:45:07 +02:00
|
|
|
|
2025-03-18 23:19:43 +02:00
|
|
|
To calculate test coverage, you have to run `go test` with `--cover`:
|
2025-03-18 21:45:07 +02:00
|
|
|
|
|
|
|
```bash
|
2025-03-18 23:19:43 +02:00
|
|
|
$ go test --cover -coverpkg=./internal/pilgrim... -covermode=count -coverprofile=./coverage/cover.out ./...
|
2025-03-18 21:45:07 +02:00
|
|
|
```
|
|
|
|
|
2025-03-18 23:19:43 +02:00
|
|
|
We only care about packages whose name starts with `pilgrim`. The rest are
|
|
|
|
ignored.
|
2025-03-18 21:45:07 +02:00
|
|
|
|
2025-03-18 23:19:43 +02:00
|
|
|
To display the coverage report, use the `cover` tool:
|
2025-03-18 21:45:07 +02:00
|
|
|
```bash
|
2025-03-18 23:19:43 +02:00
|
|
|
$ go tool cover -html=./coverage/cover.out
|
2025-03-18 22:16:42 +02:00
|
|
|
```
|
|
|
|
|
|
|
|
Alternatively, run the `test_coverage.sh` script inside the `bin` directory:
|
|
|
|
|
|
|
|
```bash
|
|
|
|
$ ./bin/test_coverage.sh
|
2025-03-15 19:36:11 +02:00
|
|
|
```
|