Before, After, and Around Test Aspects
- 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)
)
-
The
TestAspect.aroundTest
takes a scoped resource and evaluates every test within the context of the scoped function. -
There are also
TestAspect.beforeAll
,TestAspect.afterAll
,afterAllFailure
,afterAllSuccess
, andTestAspect.aroundAll
variants. -
Using
TestAspect.aroundWith
andTestAspect.aroundAllWith
we can evaluate every test or all test between two given effects,before
andafter
, where the result of thebefore
effect can be used in theafter
effect.