1172 Commits

Author SHA1 Message Date
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
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
Olivier Mengué
4f71159ca8 assert.YAMLEq: shortcut if same strings
Shortcut in assert.YAMLEq once we have validated that 'expected' is
valid YAML, and 'actual' is the exact same string.
2025-05-30 16:08:10 +02:00
Olivier Mengué
4eb688ba0c assert.JSONEq: shortcut if same strings
Shortcut in assert.JSONEq once we have validated that 'expected' is
valid JSON, and 'actual' is the exact same string.
2025-05-30 16:05:08 +02:00
ccoVeille
3c1541a3b4 Improve usage of Sprintf with Same/NotSame
Co-authored-by: Olivier Mengué <dolmen@cpan.org>
2025-05-27 23:42:48 +02:00
Bracken
c98ef6eb29
Merge pull request #1423 from stretchr/add-Helper-method-in-tests
Add Helper() method in internal mocks and assert.CollectT
2025-05-27 08:22:18 +02:00
Olivier Mengué
8f73f15d69 assert.CollectT: add Helper() method
Add Helper() method to CollectT like testing.T as we intend to add
Helper() to the assert.TestingT interface.
2025-05-25 21:56:50 +02:00
Olivier Mengué
e2ad95950e mock/tests: add method Helper() to all mock of *testing.T 2025-05-25 21:56:50 +02:00
Olivier Mengué
b534400d17 require/tests: add Helper() method to all mocks of *testing.T 2025-05-25 21:56:50 +02:00
Olivier Mengué
bf47cc8868 assert/tests: add Helper() in all testing.T mocks
Add an Helper() method to all mocks in tests of package 'assert'.
2025-05-25 21:56:50 +02:00
Ararsa
d2ddb5da5d
suite.Passed: add one more status test report (#1706)
## Summary
This test case verifies that the Passed method returns false when all
tests in the suite fail

## Changes
Added a test case to check the scenario where all tests fail.

## Motivation
This test is important to ensure that the Passed method correctly
identifies the overall failure state of a test suite when none of the
individual tests pass.

 ## Example usage 
This test can be used as a part of the test suite to validate the
behavior of the Passed method under failure conditions.

## Related issues
None
2025-05-25 10:10:11 +02:00
Olivier Mengué
83d32a3d33
Merge pull request #1747 from stretchr/dolmen/assert,require-enable-parallel-testing
assert,require: enable parallel testing on (almost) all top tests
2025-05-25 05:39:27 +02:00
Olivier Mengué
603c2a0348 assert,require: enable parallel testing on (almost) all top tests
Enable parallel testing for almost all tests in packages 'assert' and
'require' by calling t.Parallel() as the first line of the test.

A few tests are incompatible and will be fixed separately. They are
marked with a FIXME.
Incompatible tests: TestFileExists, TestNoFileExists, TestDirExists,
TestNoDirExists.

Before:
  $ go test -count=10 ./assert ./require
  ok  	github.com/stretchr/testify/assert	7.575s
  ok  	github.com/stretchr/testify/require	1.501s

After:
  $ go test -count=10 ./assert ./require
  ok  	github.com/stretchr/testify/assert	1.703s
  ok  	github.com/stretchr/testify/require	1.245s
2025-05-23 16:17:12 +02:00
Bracken
11522a0d15
Merge pull request #1746 from stretchr/dolmen/fix-CI-script-shebang
CI: fix shebang in .ci.*.sh scripts
2025-05-23 16:01:51 +02:00
ccoVeille
559d23ae66
check test failure message for Empty and NotEmpty
Only the tests are updated, code is unchanged.

Previously, the tests were checking only the result of the asserter.
Using captureTestingT helper allows to check the error message
2025-05-22 18:51:32 +02:00
Olivier Mengué
af716f8bff CI: fix shebang in .ci.*.sh scripts
Fix shebang in .ci.*.sh scripts to ease running on platforms where bash
is not in /bin/bash (ex: MacOS).
2025-05-22 11:53:03 +02:00
Bart Venter
9fc264e324
assert: add IsNotType (#1730)
Add the `IsNotType` assertion, which is the inverse
of the existing `IsType` assertion. It allows users to assert that an
object is not of a specific type.

Additionally, minor documentation improvements were made.

## Summary
This PR adds a new assertion function, `IsNotType`, to the
`testify/assert` package. It complements the existing `IsType` function
by providing a way to assert that an object is not of a specific type.

## Changes
* Added the `IsNotType` function to the `assert` package.
* Wrote unit tests for `IsNotType` to ensure correctness.
* Updated documentation to include examples and usage for `IsNotType`.

## Motivation
The `testify/assert` package already provides an `IsType` function to
check if an object is of a specific type. However, there was no built-in
way to assert that an object is **not** of a specific type. This PR
addresses that gap by introducing the `IsNotType` function, improving
the library's completeness and usability.

## Example usage
```go
assert.IsNotType(t, &MyStruct{}, actualObject)
```

## Related issues
_N/A_
2025-05-22 11:45:11 +02:00
Olivier Mengué
2035e7d7a7
Merge pull request #1739 from ccoveille-forks/asserter-gofumpt
Format assertions files with gofumpt
2025-05-22 10:38:23 +02:00
ccoVeille
9bcca2f950
Format assertions files with gofumpt 2025-05-13 14:17:27 +02:00
Bracken
016e2e9c26
Merge pull request #1671 from alexandear-org/chore/remove-deprecated-build-constraints
assert: remove deprecated build constraints
2025-05-12 15:34:44 +01:00
Bracken
296df14257
Merge pull request #1734 from stretchr/errors-asserter
Improve ErrorAs failure message when error is nil
2025-05-12 15:30:04 +01:00
ccoVeille
c2116b4194
Improve ErrorAs failure message when error is nil
Before:

    Should be in error chain:
    expected: *assert.customError
    in chain:

After:
    An error is expected but got nil.
    expected: *assert.customError

The message `An error is expected but got nil.` is the one already
reported by `assert.Error`
2025-05-08 20:48:57 +02:00
Bracken
111716d6f9
Merge pull request #1729 from siliconbrain/fix-subset-notsubset-mixed-type
fix Subset/NotSubset when calling with mixed input types
2025-05-08 13:42:27 +01:00
Dudás Ádám
121ddb9b0e clarify behavior of Subset/NotSubset when passing maps 2025-05-08 11:16:40 +00:00
Bracken
d338e951cf
Merge pull request #1681 from tsioftas/tsioftas/erroris-improve
Improve ErrorIs message when error is nil but an error was expected
2025-05-08 11:40:41 +01:00
Bracken
520d02e627
Merge pull request #1675 from architagr/doc-update-for-error-fn
Update documentation for the Error function in assert or require package
2025-05-07 13:56:33 +01:00
Archit Agarwal
01b9a87c30
Merge branch 'master' into doc-update-for-error-fn 2025-05-07 17:26:11 +05:30