Skip to main content
Version: 2.x

Before, After, and Around Test Aspects

  1. We can run an effect before, after, or around every test:
  • TestAspect.before
  • TestAspect.after
  • TestAspect.afterFailure
  • TestAspect.afterSuccess
  • TestAspect.around
import zio._
import zio.test.{ test, _ }

test("before and after") {
for {
tmp <- System.env("TEMP_DIR")
} yield assertTrue(tmp.contains("/tmp/test"))
} @@ TestAspect.before(
TestSystem.putEnv("TEMP_DIR", s"/tmp/test")
) @@ TestAspect.after(
System.env("TEMP_DIR").flatMap(deleteDir)
)
  1. The TestAspect.aroundTest takes a scoped resource and evaluates every test within the context of the scoped function.

  2. There are also TestAspect.beforeAll, TestAspect.afterAll, afterAllFailure, afterAllSuccess, and TestAspect.aroundAll variants.

  3. Using TestAspect.aroundWith and TestAspect.aroundAllWith we can evaluate every test or all test between two given effects, before and after, where the result of the before effect can be used in the after effect.

ZIO Chat

Hello! ๐Ÿ˜Š I'm your ZIO Chat Bot, designed to be your go-to resource for all things ZIO! Whether you're new to this powerful functional effect system or a seasoned pro, I'm here to assist you. ZIO empowers you to build concurrent and resilient applications in Scala, offering referential transparency, composable data types, type-safety, and more. So, go ahead and ask me anything about ZIO โ€“ I'm here to provide the answers you need! ๐Ÿค–