-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature: assert.Consistently #1087
Comments
Would for i := 1 ; i <= 15 ; i++ {
t.Logf("attempt %d", i)
time.Sleep(200*time.Millisecond)
got := myCode()
assert.Equal(t, expectation, got)
} Another concern I have is what if someone calls Edit: Actually testify could call |
Readability is subjective: I'm more interested in the prescriptiveness it would bring: setting a clear standard in tests that use testify for how to achieve this and leaving less room for interpretation for contributors. As far as your concerns about t.Error does that problem also exist for |
I think I wouldn't object to adding this. |
Maybe it should take two durations, a If the function takes longer than the tick do you run them concurrently or fail the assertion? |
Right, in the original post I had mocked it up that way but apparently messed up the var names so it's a little confusing, here's a redux: assert.Consistently(t, func() bool {
fmt.Println("my logic here")
return result()
}, testFor, tick)
Good question, I think the caller should have to instruct the test mechanism in regards to how long the actual test takes. Perhaps this would be a better signature: assert.Consistently(t, func() bool {
fmt.Println("my logic here")
return result()
}, totalTestDuration, requiredSuccesses, tick)
This fits well with my use case, because the current tests I would use these in test things like HTTP responses to a local API and this adds a slight performance testing edge to the mix which I think adds to the utility, and fits with the theme of "consistency" I think. |
Maybe, specifying a total duration and a number of successful executions feel a little over-constrained though? You can define a Consistently call which can never pass. It might just be that it needs a better name, but it sounds like It makes me lean towards something similar to how you wrote it in your first post here actually, even though that wasn't what you intended: func Consistently(t TestingT, f func() bool, every time.Duration, times int) bool Run |
Yes, this definition works too. The performance style stuff can be a consideration for later. |
Hey! I would love to implement that feature. As far as I am aware the way to do this is:
Did I understand the process correctly? Thanks a lot! |
It looks like it could be called func Repeat(t TestingT, f func() bool, every time.Duration, times int) bool However you can use the In addition, I see nothing directly related to So I'm not convinced that this is worth extending the API surface of |
In my mind this isn't intended as a utility to repeat a test, but instead it serves as the inverse of
Reads nicer than:
To provide a concrete example: https://book.kubebuilder.io/cronjob-tutorial/writing-tests#testing-your-controllers-behavior You can see how Kubebuilder uses a similar assertion provided by Gomega when testing Kubernetes Operators:
|
Any updates on this issue? I have a use case similar to @jfmyers9, for testing K8s operators. |
This changeset adds the `assert.Consistently` and it's associated functions to assert that a condition is true over the entire period of `waitFor`. This is useful when testing the behavior of asynchronous functions. Closes stretchr#1087
This changeset adds the `assert.Consistently` and it's associated functions to assert that a condition is true over the entire period of `waitFor`. This is useful when testing the behavior of asynchronous functions. Closes stretchr#1087
We have not discussed what should happen in case of failure of the condition:
|
It appears we already have So it looks like we just need to improve the documentation to link to it from the doc of Eventually*. |
Currently there is:
Now, in my case, I have a use case for a construct similar to |
I've been using testify for a long time now, I appreciate you building and maintaining this for us all 🙇
I'm looking for functionality that is inspired by other testing frameworks that would be similar to
assert.Eventually
andrequire.Eventually
, but instead would be:The point of this would be to ensure that a result is consistently occurring over a period of time. In the above example, that the fake
result()
function is returning true consistently every 200ms for 3 seconds total.Does testify already have something like this and I've just missed it?
If not, are you cool with me contributing something like this?
Thanks!
The text was updated successfully, but these errors were encountered: