Test Coverage and testify
go test -cover and the testify assertion library
Test Coverage and testify is a free Go Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Go Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
go test -cover
Add -cover to any go test command to print the coverage percentage for each package.
go test -cover ./...
// ok myapp 68.4% of statementsCoverage profile
Generate a coverage profile file and view it as HTML to see which lines are covered:
go test -coverprofile=c.out ./...
go tool cover -html=c.out -o coverage.htmlCoverage thresholds in CI
Enforce a minimum coverage with a shell one-liner:
pct=$(go test -cover ./... | awk '/coverage/ {gsub(/%/,""); print $NF}')
[ "${pct%.*}" -ge 80 ] || { echo "Coverage $pct% below 80%"; exit 1; }100% coverage is not the goal
Coverage measures which lines run, not whether assertions are meaningful. Aim for high coverage on critical paths; accept lower on unreachable error branches.
testify/assert
github.com/stretchr/testify/assert provides readable assertions that report expected/actual values automatically:
assert.Equal(t, 42, result)
assert.NoError(t, err)
assert.Contains(t, list, "item")
assert.True(t, isValid)testify/require
require is identical to assert but calls t.FailNow() on failure, stopping the test immediately (like t.Fatal).
require.NoError(t, err) // stop if err != nil
// Subsequent code only runs if err is niltestify/suite
testify/suite groups related tests in a struct with shared setup/teardown, similar to xUnit test suites:
type UserSuite struct { suite.Suite; db *DB }
func (s *UserSuite) SetupTest() { s.db = openTestDB() }
func (s *UserSuite) TestCreate() { /* ... */ }
func TestUserSuite(t *testing.T) { suite.Run(t, new(UserSuite)) }assert.ElementsMatch
Compare slices without caring about order:
assert.ElementsMatch(t, []int{3,1,2}, []int{1,2,3})assert.JSONEq
Compare JSON strings regardless of key order:
assert.JSONEq(t, `{"a":1,"b":2}`, `{"b":2,"a":1}`)Custom assertions
Wrap testify in your own assertion helpers to add domain context to failure messages:
func assertUserValid(t testing.TB, u *User) {
t.Helper()
assert.NotEmpty(t, u.Name)
assert.Positive(t, u.ID)
}Combining coverage with testify
testify does not affect coverage reporting — coverage only cares that lines execute, regardless of the assertion library used.
Quick Check
What is the difference between testify/assert and testify/require?
Recap: Coverage and testify
Key points:
- go test -cover; -coverprofile for HTML report
- assert: continue on failure; require: stop on failure
- testify/suite for grouped tests with shared setup
- High coverage is good; meaningful assertions are better
Frequently asked questions
Is the “Test Coverage and testify” lesson free?
Yes — the full text of “Test Coverage and testify” is free to read here on the web, and the Go Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Go Academy course, upgrade to CoddyKit PRO.
What will I learn in “Test Coverage and testify”?
go test -cover and the testify assertion library You practise Go Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Go Academy?
No prior experience is required. Go Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Test Coverage and testify” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Go Academy lesson?
Yes. Every Go Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Writing Unit Tests with testing
- Table-Driven Tests
- Test Doubles: Mocks and Stubs
- Test Coverage and testify