testify
testify copied to clipboard
add EventuallyWithT assertion
Summary
This adds a version of Eventually that allows to do assertions
in the condition.
Changes
- Added
EventuallyWithT
assertion. - Added
CollectT
helper struct.
Motivation
The current Eventually
only returns one summary error in case of a failure. This is not very helpful if several conditions need to be matched in the condition function. This version collects all failures of a tick and replays the last tick's failures after the timeout.
Thus the last iteration's errors can be shown to the user.
Example Usage
externalValue := false
go func() {
time.Sleep(8*time.Second)
externalValue = true
}()
assert.EventuallyWithTf(t, func(c *assert.CollectT) {
// add assertions as needed; any assertion failure will fail the current tick
assert.True(c, externalValue, "expected 'externalValue' to be true")
}, 1*time.Second, 10*time.Second, "external state has not changed to 'true'; still: %v", externalState)
Related issues
Closes #902
@boyan-soubachov if you have a minute 🙏
I implemented something similar about a year ago, this looks like a good solution. What needs to happen to get this merged? I'm running up against this use case again and would love to have this available.
Methods Reset
and Copy
of CollectT
should have been made private.
CollectT
should not have been exposed directly (this is an implementation detail) but only an subset of the testing.TB
interface in the signature of the condition
argument of EventuallyWithT
.
But all this is now part of the public API. :(
I can't understand how adding such a feature has gone into a patch release of testify. Does this gives us (maintainers) the permission to also fix it in a patch release?
Usage in the wild on GitHub: https://github.com/search?q=content%3Aassert.EventuallyWithT+language%3Ago+NOT+user%3Astretchr+NOT+path%3Aassert%2F+NOT+path%3Arequire%2F&type=code
Cc: @brackendawson
Probably the minor version should have been bumped when this was released. Fixing it should be a patch unless we chnage the API. I think we should do the right things, perhaps this discussion deserves an issue to tackle release higyne? I'm also somewhat interested in some automated testing before releases using a lot of real modules that import testify, there have been some regressions in recent releases.