Skip to main content
Version: 2.x

Repeat and Retry

There are some situations where we need to repeat a test with a specific schedule, or our tests might fail, and we need to retry them until we make sure that our tests pass. ZIO Test has the following test aspects for these scenarios:

Repeatโ€‹

The repeat test aspect takes a schedule and repeats a test based on it. The test passes if it passes every time:

import zio._
import zio.test.{ test, _ }

test("repeating a test based on the scheduler to ensure it passes every time") {
ZIO.debug("repeating successful tests")
.map(_ => assertTrue(true))
} @@ TestAspect.repeat(Schedule.recurs(5))

Retryโ€‹

If our test fails occasionally, we can retry failed tests by providing a schedule to the retry test aspect:

For example, the following test retries a maximum of five times. Once a successful assertion is made, the test passes:

import zio._
import zio.test.{ test, _ }

test("retrying a failing test based on the schedule until it succeeds") {
ZIO.debug("retrying a failing test")
.map(_ => assertTrue(true))
} @@ TestAspect.retry(Schedule.recurs(5))

Eventuallyโ€‹

  1. The eventually test aspect keeps retrying a test until it passes, regardless of how many times it fails:
import zio._
import zio.test.{ test, _ }

test("retrying a failing test until it succeeds") {
ZIO.debug("retrying a failing test")
.map(_ => assertTrue(true))
} @@ TestAspect.eventually

ZIO Chat

๐Ÿ”ฅ Ignite your ZIO journey with ZIO Chat! ๐Ÿ”ฅ Ready to unravel the intricacies of ZIO? As your trusty sidekick, I'm here to light the way. No matter if you're wandering through functional forests or traversing effectful landscapes, I've got answers to keep you on track. Your ZIO adventure starts here โ€“ ask me anything and let's explore together!