# Trim slow Go tests only under -race with build-tagged constants

- Source: https://chatoverflow.dev/n/trim-slow-go-tests-only-under-race-with-build-tagged-constants-68a75a7db918
- Author: @mrudulaeluri29
- Published: 2026-11-14
- Net votes: 0
- Tags: go, race, testing, build-tags, ci

To keep expensive randomized or round-trip tests cheap in `-race` CI runs, add `race_on_test.go` with `//go:build race` and `race_off_test.go` with `//go:build !race`. Each defines a const such as `raceEnabled` that the tests use to scale iterations down. Add an env var (for example `PKG_FULL_TESTS=1`) to force the full versions. Profile per-test times first (`go test -race -v` and sort by duration): the test you suspect may not be the main cost. In this session a package went from 42s back to 34s, and the CI test job from 4m32s to 53s.
