1197 Commits

Author SHA1 Message Date
Bracken
e8daaaca9c
Merge pull request #1665 from vyas-git/master
suite: validate method signatures and continue execution for valid tests
2025-08-29 11:16:46 +02:00
Vyas Reddy
96f97a2639
Merge branch 'master' into master 2025-08-28 23:36:22 +05:30
Vyas Reddy
5f941c88ce
Update suite/suite.go
Co-authored-by: Bracken <abdawson@gmail.com>
2025-08-28 23:36:11 +05:30
Bracken
c952903e9f
Merge pull request #1786 from brackendawson/1785-mock-argument-stringers
mock: revert to pre-v1.11.0 argument matching behavior for mutating stringers
2025-08-27 12:31:33 +02:00
Bracken Dawson
0cdb408f53
Revert "Merge pull request #1615 from DevotedHealth/mauclair-mock-match-sprintf"
This reverts commit a31a53e5b43e2c8827f61e9a13e885c64b754805, reversing
changes made to 5ac6528bffc1ed7557980c3c563caf8308568446.
2025-08-27 11:50:26 +02:00
Bracken Dawson
5e25bfb162
mock: Test that arguments with mutating stringers match
Covers the use case in issue 1785 where mock can fail to match arguments by inappropriately mutating them by calling their stringer interface.
2025-08-27 11:37:25 +02:00
Bracken
f4f5960503
Merge pull request #1783 from stretchr/ci-go1.23
CI: test also with Go 1.23
2025-08-25 16:12:46 +02:00
Olivier Mengué
af16170941 CI: test also with Go 1.23
Now that Go 1.25.0 is released, Go 1.23 must be explicitely mentioned
for testing as it was previously "oldstable".
2025-08-25 16:05:54 +02:00
Bracken
e581b36d7b
Merge pull request #1775 from mutaiib/bugfix/panic-methodcalled-mutex-release-1731
mock: avoid panic when expected type is nil in Arguments.Diff
2025-08-18 21:30:04 +02:00
mutaiib
e3d64ad5af
Update mock/mock_test.go
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>
2025-08-19 00:19:15 +05:30
mutaiib
354fc33d53 Update mock/mock.go
In short, Mock cannot match interface types with `IsType`, it only matches the values' concrete type.

Co-authored-by: Bracken <abdawson@gmail.com>
2025-08-18 21:38:15 +05:30
mutaiib
8976267da7 mock: document IsType interface limitation and add tests
- 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)
2025-08-18 21:38:15 +05:30
mutaiib
b9a23f4b6a refactor: unexport and convert Arguments.SafeName method to standalone function
- Renamed `Arguments.SafeName` to `safeTypeName`
2025-08-18 21:38:15 +05:30
mutaiib
7b2204f8a3 fix(pkg-mock): avoid panic when expected type is nil in Arguments.Diff
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.
2025-08-18 21:38:15 +05:30
Bracken
b7801fbf5c
Merge pull request #1778 from stretchr/dependabot/github_actions/actions/checkout-5
build(deps): bump actions/checkout from 4 to 5
v1.11.0
2025-08-17 17:57:51 +02:00
dependabot[bot]
69831f3b08
build(deps): bump actions/checkout from 4 to 5
Bumps [actions/checkout](https://github.com/actions/checkout) from 4 to 5.
- [Release notes](https://github.com/actions/checkout/releases)
- [Changelog](https://github.com/actions/checkout/blob/main/CHANGELOG.md)
- [Commits](https://github.com/actions/checkout/compare/v4...v5)

---
updated-dependencies:
- dependency-name: actions/checkout
  dependency-version: '5'
  dependency-type: direct:production
  update-type: version-update:semver-major
...

Signed-off-by: dependabot[bot] <support@github.com>
2025-08-12 19:06:51 +00:00
Vyas Reddy
fada016416
Merge branch 'master' into master 2025-07-04 20:36:22 +05:30
ccoVeille
a53be35c3b Improve captureTestingT helper
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.
2025-07-01 09:45:29 +02:00
3scalation
aafb604176 mock: improve formatting of error message
Co-authored-by: Olivier Mengué <dolmen@cpan.org>
2025-06-30 18:18:13 +02:00
user
7218e0390a improve error msg 2025-06-30 18:18:13 +02:00
Olivier Mengué
929a2126c2
Merge pull request #1758 from stretchr/dolmen/suite-faster-method-filtering
suite: faster methods filtering (internal refactor)
2025-06-30 18:13:45 +02:00
Olivier Mengué
bc7459ec38 suite: faster filtering of methods (-testify.m)
Refactor filtering of methods in suite.Run: the regexp given via
-testify.m flag is compiled just once, out of the loop.
2025-06-30 18:10:48 +02:00
Olivier Mengué
7d37b5c962 suite: refactor methodFilter
Use strings.HasPrefix instead of a /^Test/ regexp (compiled on every call).
2025-06-30 18:10:48 +02:00
Olivier Mengué
c58bc90e5c
Merge pull request #1764 from stretchr/dolmen/suite-refactor-stats-for-readability
suite.Run: refactor handling of stats for improved readability
2025-06-30 18:07:44 +02:00
Olivier Mengué
87101a6e4a suite.Run: refactor handling of stats
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.
2025-06-30 18:06:24 +02:00
Olivier Mengué
39b912b1d4
Merge pull request #1766 from stretchr/dolmen/assert-testing-parallel-TestFileDirExists
assert: refactor Test*FileExists and Test*DirExists tests to enable parallel testing
2025-06-30 18:03:33 +02:00
Olivier Mengué
44c0281fe0 assert/tests: improve failure reporting in Test{No,}{File,Dir}Exists
Improve error reporting in getTempSymlinkPath by displaying the
file paths.
2025-06-30 18:01:18 +02:00
Olivier Mengué
50277a850b assert/tests: simplify Test{No,}{File,Dir}Exists
In tests of the assert package, move more logic from each test into
the helper getTempSymlinkPath.
2025-06-30 18:01:18 +02:00
Olivier Mengué
d9125497d7 assert/tests: enable parallel testing for Test{,No}{File,Dir}Exists
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
2025-06-30 18:01:18 +02:00
Olivier Mengué
6c516f8b1d assert.CallerInfo: cleanup
Move the stackFrameBufferSize const which was in package scope but used
only by CallerInfo, into CallerInfo body.
2025-06-30 17:54:06 +02:00
vyas-git
fac5d473cb invalid method test signatures fails as subtest, other continues 2025-06-24 09:01:02 +05:30
Olivier Mengué
5c949551ee assert.CallerInfo: micro optimization by using LastIndexByte
Use strings.LastIndexByte instead of strings.Split to extract the
function name in CallerInfo. This reduces memory allocations.
2025-06-20 10:10:22 +02:00
renzoarreaza
b50b016f52
suite.Run: simplify running of Setup/TeardownSuite (#1769)
## 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
2025-06-19 10:18:00 +02:00
Olivier Mengué
7c2bbf9ebc
Merge pull request #1763 from stretchr/sketch/improve-equalvalues-coverage
assert.EqualValues: improve test coverage to 100%
2025-06-16 16:21:33 +02:00
Olivier Mengué
5488b2163b assert: improve EqualValues test coverage to 100%
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
2025-06-16 16:16:09 +02:00
ccoVeille
7127b60999 assert.ErrorAs: simplify retrieving the type name 2025-06-04 20:00:40 +02:00
Olivier Mengué
26b02e2f38
Merge pull request #1761 from dolmen/refactor-isEmpty-using-reflect.Value.IsZero
assert: faster and simpler isEmpty using reflect.Value.IsZero
2025-06-04 13:15:30 +02:00
Olivier Mengué
acd15f6053 assert.Empty: refactor using reflect.Value.IsZero()
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
2025-06-04 12:19:48 +02:00
Olivier Mengué
890082edf2 assert.Empty: refactor isEmpty (1)
Refactor isEmpty to extract func isEmptyValue. This allows to avoid
unwrapping/wrapping when checking pointer values.
2025-06-04 12:19:48 +02:00
Olivier Mengué
0b5b5e64f9 assert: add Benchmark_isEmpty
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!
2025-06-04 12:19:44 +02:00
Olivier Mengué
7a1b408b7c doc: improve godoc of top level 'testify' package 2025-06-03 11:53:19 +02:00
Olivier Mengué
c519b7942b assert.Empty: comprehensive doc of "Empty"-ness rules
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.
2025-06-03 00:11:08 +02:00
Bracken
5d95b18ab5
Merge pull request #1567 from stretchr/fix-dependency-cycle
deps: fix dependency cycle with objx (again)
2025-06-02 17:00:25 +02:00
Olivier Mengué
553e822bab go.mod: add comment about how to fix dep cycle with objx 2025-06-01 23:39:23 +02:00
Olivier Mengué
250eaa5e06 deps: fix dependency cycle with objx
$ 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
2025-06-01 23:39:22 +02:00
Olivier Mengué
a5d5c3395e
Merge pull request #1745 from ccoveille-forks/improve-tests-empty-notempty
assert: check test failure message for Empty and NotEmpty
2025-05-31 14:48:10 +02:00
Olivier Mengué
0ff4bb43de
Merge pull request #1751 from stretchr/dolmen/suite-fix-use-of-testing-internals
suite: cleanup use of 'testing' internals at runtime
2025-05-30 17:22:03 +02:00
Olivier Mengué
82767aed18 suite: cleanup use of 'testing' internals at runtime
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.
2025-05-30 17:17:21 +02:00
Olivier Mengué
186f1654cc
Merge pull request #1756 from stretchr/dolmen/mock-Parallel-testing
mock: enable parallel testing on internal testsuite
2025-05-30 17:15:38 +02:00
Olivier Mengué
8b3dc18b37 mock: enable parallel testing on internal testsuite
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
2025-05-30 17:12:36 +02:00