In order to ease maintenance of downstream forks of Testify that would
remove dependency on github.com/stretchr/objx we move all uses of that
module in isolated source files. A fork without that dependency could
just remove those files.
See https://github.com/stretchr/testify/issues/1752#issuecomment-2921545929
The use of objx is quite contained: it is only used in Mock.TestData().
Note that we can't just remove that method or change the return value because
that would be a breaking change.
Depending on the version of go used, context.Background returns either a context.emptyCtx or a context.Backgound since they made it into its own type. This is why the tests will fail for Go 1.18.
Co-authored-by: Bracken <abdawson@gmail.com>
- Updated IsType doc comment to clarify behavior with interface types:
- IsType matches the exact concrete type provided.
- Passing a nil interface has no type information and will never match.
- To match interface values, a non-nil concrete value must be provided.
- Added tests for interface type behavior:
- Test_Arguments_Diff_WithIsTypeArgument_InterfaceType (non-nil, matches)
- Test_Arguments_Diff_WithIsTypeArgument_InterfaceType_Failing (nil, mismatch)
Calling Name() on a nil reflect.Type would panic (and leave the mutex locked),
causing hard‑to‑diagnose crashes.
Introduce a SafeName helper that returns
"<nil>" for nil types (or t.Name() otherwise), and switch the Diff method to
use SafeName(expected.t) instead of expected.t.Name() when rendering failures.
This helper is used to capture the testing.TB interface, and compare
the log output with the expected output.
This is useful for testing and refactoring purposes.
This commit improves the helper by displaying:
- the captured and expected outputs on a newline.
- the special characters in the captured output, such as newlines and tabs.
Both help with readability.
Here is an example of the output before and after the change:
Before:
assertions_test.go:3422: Logged Error: Should be in error chain
expected: *assert.customError
in chain: "EOF" (*errors.errorString)
assertions_test.go:3422: Should log Error: Should be in error chain:
expected: *assert.customError
in chain: "EOF" (*errors.errorString)
After:
assertions_test.go:3394: Recorded Error: "Should be in error chain:\nexpected: *assert.customError\nin chain: \"EOF\" (*errors.errorString)\n"
assertions_test.go:3394: Expected Error: "Should be in error chain\nexpected: *assert.customError\nin chain: \"EOF\" (*errors.errorString)"
The new format helps to identify the differences:
- the missing colon after "Should be in error chain"
- the extra newline in the captured output
Note: I spent 10 minutes on this change, because I lost 10 minutes in
finding the differences between the captured and expected output on a
refactoring I was doing.
Refactor handling of stats in suite.Run with the goal of reducing
indented blocks to improve readability.
To achieve this, the SuiteInformation methods now handle being called
with a nil receiver to work as noop. This allows to call them from
suite.Run without nil checks blocks, so with improved readability.
In package assert, fix TestFileExists, TestNoFileExists, TestDirExists, TestNoDirExists
to be able to run in parallel:
- use t.TempDir() as the storage location for temporary created
symlinks. This also allows the cleanup of that storage to be
automatically handled by "go test". testing.T.TempDir is available
since go1.15.
- enable parallel testing on each test
## Summary
Improve readability of suite.Run by moving the running of SetupSuite
outside of the loop iterating over the (test) methods.
This also allows for other simplifications further down in the code.
## Changes
- Move SetupSuite to outside the loop
- Don't run Setup/TeardownSuite if no tests are found (not new
behaviour, but new check)
- Remove variable to keep track of wether SetupSuite was executed or not
## Motivation
This is a subset of the changes I made under PR #1749. It was suggested
by @dolmen to open a separate PR for this part.
## Related issues
N/A
Extend TestNotEqualValues to also test EqualValues by leveraging the fact
that they are inverse functions. The same test cases are used to verify
that EqualValues returns the opposite result of NotEqualValues.
This change ensures both success and failure paths are tested for
EqualValues, covering the error formatting and failure reporting code
that was previously untested.
Coverage improvement:
- EqualValues: 57.1% → 100.0%
- Overall package: 68.4% → 68.5%
The test function was renamed to TestEqualValuesAndNotEqualValues to
reflect its dual purpose while maintaining all existing test logic.
Co-Authored-By: sketch <hello@sketch.dev>
Change-ID: s30d4192b08186d88k
Simplify isEmptyValue (used by assert.Empty) by checking early if the
value is the zero value of the type using reflect.Value.IsZero
(available since Go 1.13, so after the initial assert.Empty implementation).
isEmpty is now faster.
go test -bench Benchmark_isEmpty
goos: darwin
goarch: arm64
pkg: github.com/stretchr/testify/assert
cpu: Apple M2
Before:
Benchmark_isEmpty-8 15841243 77.27 ns/op 8 B/op 1 allocs/op
After
Benchmark_isEmpty-8 50665512 21.08 ns/op 0 B/op 0 allocs/op
Add a benchmark that shows that isEmpty does memory allocations.
$ go test -bench Benchmark_isEmpty
goos: darwin
goarch: arm64
pkg: github.com/stretchr/testify/assert
cpu: Apple M2
Benchmark_isEmpty-8 15074973 76.73 ns/op 8 B/op 1 allocs/op
We can do better!
Document the assert.Empty rules more comprehensively. This exposes our
quirks to the user to avoid wrong expectations.
Add many many many more test cases that document edges cases and will allow
to catch breaking changes and avoid regressions.
$ go mod edit -dropexclude=github.com/stretchr/testify@v1.8.0 -exclude=github.com/stretchr/testify@v1.8.4
$ go mod tidy
See https://github.com/stretchr/objx/pull/140
Cleanup runtime use of stdlib's testing internals which was required for older
Go versions.
Note: we are still using testing.RunTests in the suite's test suite for
now.
Add t.Parallel() to all package-level tests of the 'mock' package.
Result: faster tests results.
Before:
$ go test -count=10 ./mock
ok github.com/stretchr/testify/mock 0.631s
After:
$ go test -count=10 ./mock
ok github.com/stretchr/testify/mock 0.426s