Integrate race and coverage checks into CI
Integrate race and coverage checks into CI
Continuous integration (CI) runs reproducible checks for each change. In a module, use go vet ./..., go test -count=1 -timeout=60s ./..., then go test -race -count=1 -timeout=60s -covermode=atomic -coverprofile=coverage.out ./.... Inspect coverage with go tool cover -func=coverage.out; retain reports as CI artifacts. Race instrumentation requires a supported platform and toolchain. Pin the Go version and let nonzero exit codes fail the job.
Create the files shown below in a fresh directory, then run go test -count=1 -v ./... there. The separator lines are file labels, not source code. Go 1.23 is used for these local examples.
--- go.mod ---module example.test/lesson
go 1.23
--- lesson.go ---package lesson
func Shipping(free bool) int { if free { return 0 } return 5}
--- lesson_test.go ---package lesson
import "testing"
func TestLesson(t *testing.T) { if Shipping(true) != 0 || Shipping(false) != 5 { t.Fatal("shipping contract") }}Run the coverage command above on this small package. Both return branches execute, so statement coverage is 100% in this source. Coverage measures executed instrumented statements, not assertion quality, branch combinations, security or universal correctness. A test that calls Shipping without checking the result can still cover statements. Use a risk-based threshold, not an arbitrary global target.
Expected result (timings and formatting may vary; essential text shown):
--- PASS: TestLessonInspect a concurrent implementation under race and coverage
--- go.mod ---module example.test/lesson
go 1.23
--- lesson.go ---package lesson
import "sync"
type Counter struct { mu sync.Mutex n int}
func (c *Counter) Add() { c.mu.Lock(); defer c.mu.Unlock(); c.n++ }func (c *Counter) Value() int { c.mu.Lock(); defer c.mu.Unlock(); return c.n }
--- lesson_test.go ---package lesson
import ( "sync" "testing")
func TestLesson(t *testing.T) { var c Counter var wg sync.WaitGroup for i := 0; i < 4; i++ { wg.Add(1) go func() { defer wg.Done(); c.Add() }() } wg.Wait() if c.Value() != 4 { t.Fatal(c.Value()) }}Atomic coverage mode records concurrent execution counts safely; it does not make the application race-free. The race detector separately observes conflicting accesses. -count=1 disables successful-test result caching. Use a separate scheduled job for bounded fuzzing and a provisioned disposable database for integration. Do not mask failures with shell constructs such as || true. CI provider configuration depends on the repository and is not included here.
Expected result (timings and formatting may vary; essential text shown):
--- PASS: TestLessonTry it locally
Implement a small function that returns an error for a negative quantity and otherwise returns twice the quantity (use small integers). Test both paths and inspect its coverage report using the CI commands. Use a fresh module with the same go.mod shown above; run go test -count=1 -v ./...
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Advanced testing 07: Integrate race and coverage checks into CI
Check the lesson objective before moving on.
What does 100% statement coverage establish?
Why keep a race job even with high coverage?