testify icon indicating copy to clipboard operation
testify copied to clipboard

add EventuallyWithT assertion

Open tobikris opened this issue 2 years ago • 1 comments

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

tobikris avatar Sep 13 '22 23:09 tobikris

@boyan-soubachov if you have a minute 🙏

mrIncompetent avatar Feb 10 '23 11:02 mrIncompetent

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.

l-abels avatar Apr 14 '23 23:04 l-abels

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. :(

dolmen avatar Jul 22 '23 07:07 dolmen

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

dolmen avatar Jul 22 '23 07:07 dolmen

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.

brackendawson avatar Jul 23 '23 16:07 brackendawson