Skip to content

[go-fan] Go Module Review: stretchr/testify #4462

@github-actions

Description

@github-actions

🐹 Go Fan Report: stretchr/testify

Module Overview

testify is the de facto standard Go testing toolkit, providing rich assertion libraries (assert, require), mock support (mock), and test suites (suite). It dramatically improves test readability and error messages over the raw testing package.

Version in use: v1.11.1 (latest)

Current Usage in gh-aw-mcpg

This project is a heavy testify user — it's the backbone of the entire test suite:

  • Files: 182 test files import testify
  • Sub-packages used:
    • assert — 175 import occurrences
    • require — 159 import occurrences
    • mock0 uses
    • suite0 uses
  • Total assert/require calls: ~8,433 across the codebase
  • Bound asserter pattern (assert.New(t)): used in 30 files; 152 files use non-bound form

The project is already on the latest version and makes excellent use of the core APIs. The high test coverage and consistent use of require for critical checks vs assert for non-critical ones reflects good practice.

Research Findings

Best Practices from Maintainers

  • Prefer assert.NoError(t, err) over assert.Nil(t, err) — produces much better failure messages: "Received unexpected error: <err>" vs just "Expected nil"
  • Prefer assert.Error(t, err) over assert.NotNil(t, err) for error assertions
  • Use bound asserters (assert := assert.New(t)) when a function has many assertions to reduce t repetition
  • Use assert.ErrorIs / assert.ErrorAs for sentinel error checking (v1.10+)

Improvement Opportunities

🏃 Quick Wins

~22 uses of Nil/NotNil on error values that should use semantic error assertions:

Current Pattern Better Pattern Benefit
require.Nil(t, err) require.NoError(t, err) Better failure message
assert.Nil(t, err) assert.NoError(t, err) Better failure message
require.NotNil(t, err) require.Error(t, err) Clearer intent

Examples found:

// internal/middleware/jqschema_test.go:117
require.Nil(jqSchemaCompileErr, "jq schema filter must compile without error")
// → require.NoError(t, jqSchemaCompileErr, "jq schema filter must compile without error")

// internal/mcp/http_transport_test.go:982
require.Nil(t, resp.Error, "response should not contain an error after reconnect")
// → require.NoError(t, resp.Error, ...)  (if resp.Error implements error)

Files affected:

  • internal/middleware/jqschema_test.go
  • internal/mcp/http_transport_test.go
  • internal/mcp/http_connection_test.go
  • internal/launcher/getorlaunch_timeout_test.go
  • internal/launcher/getorlaunchforsession_test.go
  • and ~15 more

✨ Feature Opportunities

assert.ErrorIs and assert.ErrorAs (available since v1.10, already on v1.11.1):

  • The project uses EqualError (13 uses) and ErrorContains for error message checks
  • For sentinel errors (errors.Is-style checks), assert.ErrorIs gives cleaner tests and better error messages
  • Example: replace assert.EqualError(t, err, someErr.Error()) with assert.ErrorIs(t, err, someErr)

testify/mock package (available, currently unused):

  • If interfaces need mocking in future tests, the mock package is already in the dependency tree — no new dep required

📐 Best Practice Alignment

Inconsistent bound vs non-bound asserter style: The AGENTS.md recommends bound asserters for tests with multiple assertions, but only 30/182 test files use them. This is a minor inconsistency — not a bug, but standardizing would reduce t argument noise. The codebase mixes both styles freely.

assert.Nil(result, "msg") without t: Some tests use bound asserter form without calling assert.New(t) first (inheriting from outer scope). These are fine where the bound asserter was set up, but worth double-checking for correctness.

🔧 General Improvements

  • assert.ElementsMatch is already well-used (61 occurrences) — great for unordered slice comparisons ✅
  • assert.JSONEq usage is present — good for JSON comparison without formatting concerns ✅
  • EqualError (13 uses) vs ErrorContains — both used appropriately ✅

Recommendations

  1. [Low effort, high value] Replace assert.Nil(t, err)assert.NoError(t, err) and require.Nil(t, err)require.NoError(t, err) throughout the codebase (~22 occurrences). Can be done with a targeted sed/gorename pass.

  2. [Low effort] Replace assert.NotNil(t, err)assert.Error(t, err) for the few error-checking cases.

  3. [Consider] Evaluate assert.ErrorIs for sentinel error tests where EqualError currently compares error strings.

Next Steps

  • Run golangci-lint run --enable=testifylint to automatically detect all testify anti-patterns (testifylint is currently disabled in .golangci.yml due to "requiring extensive test refactoring")
  • Consider enabling testifylint as a non-blocking lint suggestion to catch new anti-patterns going forward

Generated by Go Fan 🐹
Round §24878212116 — module 5 in rotation

References:

Note

🔒 Integrity filter blocked 7 items

The following items were blocked because they don't meet the GitHub integrity level.

  • search_repositories search_repositories: has lower integrity than agent requires. The agent cannot read data with integrity below "unapproved".
  • https://github.com/santhosh-tekuri/jsonschema search_repositories: has lower integrity than agent requires. The agent cannot read data with integrity below "unapproved".
  • https://github.com/stretchr/testify search_repositories: has lower integrity than agent requires. The agent cannot read data with integrity below "unapproved".
  • stretchr/testify@5f80e4a list_commits: has lower integrity than agent requires. The agent cannot read data with integrity below "unapproved".
  • wazero/wazero@5cb4bb3 list_commits: has lower integrity than agent requires. The agent cannot read data with integrity below "unapproved".
  • santhosh-tekuri/jsonschema@180cde3 list_commits: has lower integrity than agent requires. The agent cannot read data with integrity below "unapproved".
  • https://github.com/stretchr/testify/releases/tag/v1.11.1 get_latest_release: has lower integrity than agent requires. The agent cannot read data with integrity below "unapproved".

To allow these resources, lower min-integrity in your GitHub frontmatter:

tools:
  github:
    min-integrity: approved  # merged | approved | unapproved | none

Generated by Go Fan · ● 808.8K ·

  • expires on May 1, 2026, 7:45 AM UTC

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions