From 888a87ab86899a01ae8012b2b4a3b30a4f4172f7 Mon Sep 17 00:00:00 2001 From: "Miroslav V. Vasilev" Date: Tue, 18 Mar 2025 23:19:43 +0200 Subject: [PATCH] Correct testing method and documentation --- bin/test_coverage.sh | 6 ++---- docs/TESTS.md | 19 ++++++------------- 2 files changed, 8 insertions(+), 17 deletions(-) diff --git a/bin/test_coverage.sh b/bin/test_coverage.sh index f019bc1..d419ef6 100755 --- a/bin/test_coverage.sh +++ b/bin/test_coverage.sh @@ -3,11 +3,9 @@ pushd $(dirname "$0")/.. # run from root dir rm -rf ./coverage mkdir ./coverage/ -go build -cover -o target/pilgrim_coverage main.go - chmod 777 ./target/pilgrim_coverage -GOCOVERDIR=coverage ./target/pilgrim_coverage +go test --cover -coverpkg=./internal/pilgrim... -covermode=count -coverprofile=./coverage/cover.out ./... -go tool covdata percent -i=coverage +go tool cover -html=./coverage/cover.out popd # switch back to dir we started from \ No newline at end of file diff --git a/docs/TESTS.md b/docs/TESTS.md index 4117004..eb54819 100644 --- a/docs/TESTS.md +++ b/docs/TESTS.md @@ -22,25 +22,18 @@ $ go test ./... ### Coverage -To calculate test coverage, you have to build a special coverage binary and -execute it. To do this, run the following in the root directory: +To calculate test coverage, you have to run `go test` with `--cover`: ```bash -$ go build -cover -o target/pilgrim_coverage main.go +$ go test --cover -coverpkg=./internal/pilgrim... -covermode=count -coverprofile=./coverage/cover.out ./... ``` -To calculate the coverage, execute the binary. You must setup the `GOCOVERDIR` -environment variable in order to instruct the go coverage tool where to place -the coverage data. The standard place for this in `pilgrim` is `coverage`: -```bash -$ chmod 777 ./target/pilgrim_coverage -$ GOCOVERDIR=coverage ./target/pilgrim_coverage -``` +We only care about packages whose name starts with `pilgrim`. The rest are +ignored. -This will produce several files containing the coverage data. To turn this into -a human-readable format, use the go coverage tool: +To display the coverage report, use the `cover` tool: ```bash -$ go tool covdata percent -i=coverage +$ go tool cover -html=./coverage/cover.out ``` Alternatively, run the `test_coverage.sh` script inside the `bin` directory: