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
## 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
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
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
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_
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`