📖 4 min read (~ 800 words).

Condition

Expressing Assertions Using Conditions

Assertions

GoDoc

All links point to https://pkg.go.dev/github.com/go-openapi/testify/v2

This domain exposes 4 functionalities.

Condition

Condition uses a Comparison to assert a complex condition.

Examples
	assertions.Condition(t, func() bool { return myCondition })
	success:  func() bool { return true }
	failure:  func() bool { return false }

Eventually

Eventually asserts that given condition will be met in waitFor time, periodically checking target function each tick.

Examples
	assertions.Eventually(t, func() bool { return true; }, time.Second, 10*time.Millisecond)
	success:  func() bool { return true }, 100*time.Millisecond, 20*time.Millisecond
	failure:  func() bool { return false }, 100*time.Millisecond, 20*time.Millisecond

EventuallyWithT

EventuallyWithT asserts that given condition will be met in waitFor time, periodically checking target function each tick. In contrast to Eventually, it supplies a CollectT to the condition function, so that the condition function can use the CollectT to call other assertions. The condition is considered “met” if no errors are raised in a tick. The supplied CollectT collects all errors from one tick (if there are any). If the condition is not met before waitFor, the collected errors of the last tick are copied to t.

Examples
	externalValue := false
	go func() {
		time.Sleep(8*time.Second)
		externalValue = true
	}()
	assertions.EventuallyWithT(t, func(c *assertions.CollectT) {
		// add assertions as needed; any assertion failure will fail the current tick
		assertions.True(c, externalValue, "expected 'externalValue' to be true")
	}, 10*time.Second, 1*time.Second, "external state has not changed to 'true'; still false")
	success: func(c *CollectT) { True(c,true) }, 100*time.Millisecond, 20*time.Millisecond
	failure: func(c *CollectT) { False(c,true) }, 100*time.Millisecond, 20*time.Millisecond

Never

Never asserts that the given condition doesn’t satisfy in waitFor time, periodically checking the target function each tick.

Examples
	assertions.Never(t, func() bool { return false; }, time.Second, 10*time.Millisecond)
	success:  func() bool { return false }, 100*time.Millisecond, 20*time.Millisecond
	failure:  func() bool { return true }, 100*time.Millisecond, 20*time.Millisecond


Generated with github.com/go-openapi/testify/v2/codegen