$npx -y skills add samber/cc-skills-golang --skill golang-stretchr-testifyComprehensive guide to stretchr/testify for Golang testing. Covers assert, require, mock, and suite packages in depth. Use when writing tests with testify, creating mocks, setting up test suites, or choosing between assert and require. Covers testify assertions, mock expectations
| 1 | **Persona:** You are a Go engineer who treats tests as executable specifications. You write tests to constrain behavior and make failures self-explanatory — not to hit coverage targets. |
| 2 | |
| 3 | **Modes:** |
| 4 | |
| 5 | - **Write mode** — adding new tests or mocks to a codebase. |
| 6 | - **Review mode** — auditing existing test code for testify misuse. |
| 7 | |
| 8 | # stretchr/testify |
| 9 | |
| 10 | testify complements Go's `testing` package with readable assertions, mocks, and suites. It does not replace `testing` — always use `*testing.T` as the entry point. |
| 11 | |
| 12 | This skill is not exhaustive. Please refer to library documentation and code examples for more information. For Go package docs, symbols, versions, importers, and known vulnerabilities, → See `samber/cc-skills-golang@golang-pkg-go-dev` skill (`godig`) — prefer it over Context7 for Go package facts. To navigate this library's usage in your own code (definitions, call sites, diagnostics), → See `samber/cc-skills-golang@golang-gopls` skill (`gopls`). Context7 remains a fallback for docs not indexed on pkg.go.dev. |
| 13 | |
| 14 | ## assert vs require |
| 15 | |
| 16 | Both offer identical assertions. The difference is failure behavior: |
| 17 | |
| 18 | - **assert**: records failure, continues — see all failures at once |
| 19 | - **require**: calls `t.FailNow()` — use for preconditions where continuing would panic or mislead |
| 20 | |
| 21 | Use `assert.New(t)` / `require.New(t)` for readability. Name them `is` and `must`: |
| 22 | |
| 23 | ```go |
| 24 | func TestParseConfig(t *testing.T) { |
| 25 | is := assert.New(t) |
| 26 | must := require.New(t) |
| 27 | |
| 28 | cfg, err := ParseConfig("testdata/valid.yaml") |
| 29 | must.NoError(err) // stop if parsing fails — cfg would be nil |
| 30 | must.NotNil(cfg) |
| 31 | |
| 32 | is.Equal("production", cfg.Environment) |
| 33 | is.Equal(8080, cfg.Port) |
| 34 | is.True(cfg.TLS.Enabled) |
| 35 | } |
| 36 | ``` |
| 37 | |
| 38 | **Rule**: `require` for preconditions (setup, error checks), `assert` for verifications. Never mix randomly. |
| 39 | |
| 40 | ## Core Assertions |
| 41 | |
| 42 | ```go |
| 43 | is := assert.New(t) |
| 44 | |
| 45 | // Equality |
| 46 | is.Equal(expected, actual) // DeepEqual + exact type |
| 47 | is.NotEqual(unexpected, actual) |
| 48 | is.EqualValues(expected, actual) // converts to common type first |
| 49 | is.EqualExportedValues(expected, actual) |
| 50 | |
| 51 | // Nil / Bool / Emptiness |
| 52 | is.Nil(obj) is.NotNil(obj) |
| 53 | is.True(cond) is.False(cond) |
| 54 | is.Empty(collection) is.NotEmpty(collection) |
| 55 | is.Len(collection, n) |
| 56 | |
| 57 | // Contains (strings, slices, map keys) |
| 58 | is.Contains("hello world", "world") |
| 59 | is.Contains([]int{1, 2, 3}, 2) |
| 60 | is.Contains(map[string]int{"a": 1}, "a") |
| 61 | |
| 62 | // Comparison |
| 63 | is.Greater(actual, threshold) is.Less(actual, ceiling) |
| 64 | is.Positive(val) is.Negative(val) |
| 65 | is.Zero(val) |
| 66 | |
| 67 | // Errors |
| 68 | is.Error(err) is.NoError(err) |
| 69 | is.ErrorIs(err, ErrNotFound) // walks error chain |
| 70 | is.ErrorAs(err, &target) |
| 71 | is.ErrorContains(err, "not found") |
| 72 | |
| 73 | // Type |
| 74 | is.IsType(&User{}, obj) |
| 75 | is.Implements((*io.Reader)(nil), obj) |
| 76 | ``` |
| 77 | |
| 78 | **Argument order**: always `(expected, actual)` — swapping produces confusing diff output. |
| 79 | |
| 80 | ## Advanced Assertions |
| 81 | |
| 82 | ```go |
| 83 | is.ElementsMatch([]string{"b", "a", "c"}, result) // unordered comparison |
| 84 | is.InDelta(3.14, computedPi, 0.01) // float tolerance |
| 85 | is.JSONEq(`{"name":"alice"}`, `{"name": "alice"}`) // ignores whitespace/key order |
| 86 | is.WithinDuration(expected, actual, 5*time.Second) |
| 87 | is.Regexp(`^user-[a-f0-9]+$`, userID) |
| 88 | |
| 89 | // Async polling |
| 90 | is.Eventually(func() bool { |
| 91 | status, _ := client.GetJobStatus(jobID) |
| 92 | return status == "completed" |
| 93 | }, 5*time.Second, 100*time.Millisecond) |
| 94 | |
| 95 | // Async polling with rich assertions |
| 96 | is.EventuallyWithT(func(c *assert.CollectT) { |
| 97 | resp, err := client.GetOrder(orderID) |
| 98 | assert.NoError(c, err) |
| 99 | assert.Equal(c, "shipped", resp.Status) |
| 100 | }, 10*time.Second, 500*time.Millisecond) |
| 101 | ``` |
| 102 | |
| 103 | ## testify/mock |
| 104 | |
| 105 | Mock interfaces to isolate the unit under test. E |