testify icon indicating copy to clipboard operation
testify copied to clipboard

`NeverWithT` for using assertions inside `Never`

Open samox73 opened this issue 5 months ago • 3 comments

Description

If we want to check multiple conditions inside Never, currently, we have to do

assert.Never(t, func() bool {
  if GetVal1() == 1 {
    return true
  }
  if GetVal2() == 2 {
    return true
  }
  return false
}, ...)

or

assert.Never(t, func() bool {
  val1 := GetVal1()
  val2 := GetVal2()
  return val1 == 1 || val2 == 2
}, ...)

or

assert.Never(t, func() bool {
  return GetVal1() == 1
}, ...)
assert.Never(t, func() bool {
  return GetVal2() == 2
}, ...)

however for N conditions

  1. has the problem of N if statements
  2. has the problem of chaining N comparisons in the return statement
  3. is slower, because we wait N times as long

Proposed solution

For calling assertions inside Eventually, there is EventuallyWithT, e.g.

assert.EventuallyWithT(t, func(c *assert.CollectT) {
  assert.Equal(c, 2, GetVal())
}, ...)

It would be cool if we could write:

assert.NeverWithT(t, func(c *assert.CollectT) {
  assert.Equal(c, 1, GetVal1())
  assert.Equal(c, 2, GetVal2())
}, ...)

However, regarding the semantics: The function would need to know that ALL assertions failed, which is technically not possible right now to my understanding.

Use case

I'm writing a lot of Kubernetes integration tests for operators, where we often need to check that async operations DO or DO NOT change the Kubernetes states -- via Eventually and Never.

samox73 avatar Jul 24 '25 13:07 samox73

I'm writing a lot of Kubernetes integration tests for operators, where we often need to check that async operations DO or DO NOT change the Kubernetes states -- via Eventually and Never.

What's the difference here between use of Never vs time.Sleep(...) followed by the assertions?

brackendawson avatar Aug 18 '25 18:08 brackendawson

Good point, some cases can be covered like this. However when the order of processing operations cannot be predicted, I would really want to know that certain conditions never occurred over a period of time, e.g. 5s in an interval of 500ms, not just at one point in the future.

I guess the user could create a ticker and invert their assertions, but that's the reason why I opened the issue.

samox73 avatar Aug 19 '25 13:08 samox73

That's not what "never" means though, the condition most certainly could have happened but not been sampled. In my opinion the Never assertion should not have been added to testify for this reason. Eventually is useful because it allows you to assert the outcome of an asynchronous function in close to as short a time as it will take. I have the same beef with Consistently (the inverse of Never).

brackendawson avatar Aug 19 '25 17:08 brackendawson