{"id":16092268,"url":"https://github.com/alecthomas/assert","last_synced_at":"2025-05-15T20:07:29.495Z","repository":{"id":2659967,"uuid":"47095688","full_name":"alecthomas/assert","owner":"alecthomas","description":"A simple assertion library using Go generics","archived":false,"fork":false,"pushed_at":"2025-05-10T03:35:48.000Z","size":132,"stargazers_count":160,"open_issues_count":8,"forks_count":10,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-05-10T04:27:33.645Z","etag":null,"topics":["assert","generics","go","golang","testing"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/alecthomas.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":"COPYING","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null},"funding":{"github":["alecthomas"]}},"created_at":"2015-11-30T04:42:32.000Z","updated_at":"2025-05-05T09:52:40.000Z","dependencies_parsed_at":"2024-05-01T02:44:56.003Z","dependency_job_id":"abbe3d60-103b-4c26-9493-d55602ae4e71","html_url":"https://github.com/alecthomas/assert","commit_stats":{"total_commits":50,"total_committers":5,"mean_commits":10.0,"dds":0.07999999999999996,"last_synced_commit":"96885dec03fb9107de54aba088c8e366e1d67ce9"},"previous_names":[],"tags_count":28,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alecthomas%2Fassert","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alecthomas%2Fassert/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alecthomas%2Fassert/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alecthomas%2Fassert/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/alecthomas","download_url":"https://codeload.github.com/alecthomas/assert/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254414499,"owners_count":22067272,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["assert","generics","go","golang","testing"],"created_at":"2024-10-09T16:06:28.722Z","updated_at":"2025-05-15T20:07:24.477Z","avatar_url":"https://github.com/alecthomas.png","language":"Go","funding_links":["https://github.com/sponsors/alecthomas"],"categories":[],"sub_categories":[],"readme":"# A simple assertion library using Go generics\n\n[![PkgGoDev](https://pkg.go.dev/badge/github.com/alecthomas/assert/v2)](https://pkg.go.dev/github.com/alecthomas/assert/v2) [![CI](https://github.com/alecthomas/assert/actions/workflows/ci.yml/badge.svg)](https://github.com/alecthomas/assert/actions/workflows/ci.yml) \n[![Go Report Card](https://goreportcard.com/badge/github.com/alecthomas/assert/v2)](https://goreportcard.com/report/github.com/alecthomas/assert/v2) [![Slack chat](https://img.shields.io/static/v1?logo=slack\u0026style=flat\u0026label=slack\u0026color=green\u0026message=gophers)](https://gophers.slack.com/messages/CN9DS8YF3)\n\n\nThis library is inspired by testify/require, but with a significantly reduced\nAPI surface based on empirical use of that package.\n\nIt also provides much nicer diff output, eg.\n\n```\n=== RUN   TestFail\n    assert_test.go:14: Expected values to be equal:\n         assert.Data{\n        -  Str: \"foo\",\n        +  Str: \"far\",\n           Num: 10,\n         }\n--- FAIL: TestFail (0.00s)\n```\n\n## API\n\nImport then use as `assert`:\n\n```go\nimport \"github.com/alecthomas/assert/v2\"\n```\n\nThis library has the following API. For all functions, `msgAndArgs` is used to\nformat error messages using the `fmt` package.\n\n```go\n// Equal asserts that \"expected\" and \"actual\" are equal using google/go-cmp.\n//\n// If they are not, a diff of the Go representation of the values will be displayed.\nfunc Equal[T comparable](t testing.TB, expected, actual T, msgAndArgs ...interface{})\n\n// NotEqual asserts that \"expected\" is not equal to \"actual\" using google/go-cmp.\n//\n// If they are equal the expected value will be displayed.\nfunc NotEqual[T comparable](t testing.TB, expected, actual T, msgAndArgs ...interface{})\n\n// Zero asserts that a value is its zero value.\nfunc Zero[T comparable](t testing.TB, value T, msgAndArgs ...interface{})\n\n// NotZero asserts that a value is not its zero value.\nfunc NotZero[T comparable](t testing.TB, value T, msgAndArgs ...interface{})\n\n// Contains asserts that \"haystack\" contains \"needle\".\nfunc Contains(t testing.TB, haystack string, needle string, msgAndArgs ...interface{})\n\n// NotContains asserts that \"haystack\" does not contain \"needle\".\nfunc NotContains(t testing.TB, haystack string, needle string, msgAndArgs ...interface{})\n\n// EqualError asserts that either an error is non-nil and that its message is what is expected,\n// or that error is nil if the expected message is empty.\nfunc EqualError(t testing.TB, err error, errString string, msgAndArgs...interface{})\n\n// Error asserts that an error is not nil.\nfunc Error(t testing.TB, err error, msgAndArgs ...interface{})\n\n// NoError asserts that an error is nil.\nfunc NoError(t testing.TB, err error, msgAndArgs ...interface{})\n\n// IsError asserts than any error in \"err\"'s tree matches \"target\".\nfunc IsError(t testing.TB, err, target error, msgAndArgs ...interface{})\n\n// NotIsError asserts than no error in \"err\"'s tree matches \"target\".\nfunc NotIsError(t testing.TB, err, target error, msgAndArgs ...interface{})\n\n// Panics asserts that the given function panics.\nfunc Panics(t testing.TB, fn func(), msgAndArgs ...interface{})\n\n// NotPanics asserts that the given function does not panic.\nfunc NotPanics(t testing.TB, fn func(), msgAndArgs ...interface{})\n\n// Compare two values for equality and return true or false.\nfunc Compare[T any](t testing.TB, x, y T) bool\n\n// True asserts that an expression is true.\nfunc True(t testing.TB, ok bool, msgAndArgs ...interface{})\n\n// False asserts that an expression is false.\nfunc False(t testing.TB, ok bool, msgAndArgs ...interface{})\n```\n\n## Evaluation process\n\nOur empirical data of testify usage comes from a monorepo with around 50K lines\nof tests.\n\nThese are the usage counts for all testify functions, normalised to the base\n(not `Printf()`) non-negative(not `No(t)?`) case for each core function.\n\n```text\n2240 Error\n1314 Equal\n 219 True\n 210 Nil\n 167 Empty\n 107 Contains\n  79 Len\n  61 False\n  24 EqualValues\n  20 EqualError\n  17 Zero\n  15 Fail\n  15 ElementsMatch\n   9 Panics\n   7 IsType\n   6 FileExists\n   4 JSONEq\n   3 PanicsWithValue\n   3 Eventually\n```\n\nThe decision for each function was:\n\n### Keep\n\n- `Error(t, err)` -\u003e frequently used, keep\n- `Equal(t, expected, actual)` -\u003e frequently used, keep but make type safe\n- `True(t, expr)` -\u003e frequently used, keep\n- `False(t, expr)` -\u003e frequently used, keep\n- `Empty(t, thing)` -\u003e `require.Equal(t, len(thing), 0)`\n- `Contains(t, haystack string, needle string)` - the only variant used in our codebase, keep as concrete type\n- `Zero(t, value)` -\u003e make type safe, keep\n- `Panics(t, f)` -\u003e useful, keep\n- `EqualError(t, a, b)` -\u003e useful, keep\n- `Nil(t, value)` -\u003e frequently used, keep\n\n### Not keeping, replace with ...\n\n- `ElementsMatch(t, a, b)` - use [peterrk/slices](https://github.com/peterrk/slices) or stdlib sort support once it lands.\n- `IsType(t, a, b)` -\u003e `require.Equal(t, reflect.TypeOf(a).String(), reflect.TypeOf(b).String())`\n- `FileExists()` -\u003e very little use, drop\n- `JSONEq()` -\u003e very little use, drop\n- `PanicsWithValue()` -\u003e very little use, drop\n- `Eventually()` -\u003e very little use, drop\n- `Contains(t, haystack []T, needle T)` - very little use, replace with\n- `Contains(t, haystack map[K]V, needle K)` - very little use, drop\n- `Len(t, v, n)` -\u003e cannot be implemented as a single function with generics`Equal(t, len(v), n)`\n- `EqualValues()` - `Equal(t, TYPE(a), TYPE(b))`\n- `Fail()` -\u003e `t.Fatal()`\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falecthomas%2Fassert","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Falecthomas%2Fassert","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falecthomas%2Fassert/lists"}