This part covers the ZIO tests. It is based on the documents of, Testing Effects, Testing Use Case and ZIO test effects. ZIO makes tests first-class objects in that tests are values that can be passed, transformed and composed. ZIO tests are effects.

Testing Effects

The ZIO data type has an R type parameter that is used to describe the type of environment required by the effect. For pure values, use ZIO.access[R] to retrieve it. For effect value, use ZIO.accessM[R].An effect accessed from the environment is called an environment effect. The idea behind environment effects is to program to an interface, not an implementation. You can use a test implementation for testing.

ZIO Test

In ZIO test, all tests are immutable values that can be pure values or effects that can be defined using test or testM. ZIO also provides test evnironmental effects for Clock, Consoel, System, and Random. The Assertion class defines many assertion combinators.

Additionally, ZIO test supports test aspects @@ for timeout or different platform. It supports property based testing and automatic derivation of generators using Magnolia module.

Constructing Tests

Use suite to creat a suite of tests. A suite can contain many test, testM and suite.

A test expects two arugments: a string label and an asertion of type ZIO[R, E, TestResult]. Use aeert or assertM method to create an assertion. Both of them accept a value of type A or a value of Assertaion[A].

use Assertion object methods to create Assertion[A]. Prefdeind methods incude equalTo, isFalse, isTrue, contains, throw and more. The result can be composed using &&, ||, and negate. For nested structure, methods can be cmposed using nested calls as isRight(isSome(equalTo(2))). the hasField method takes a field name, a mapping method, and an assertion.

Use for-comprehension for testM tests.

Run Tests

To run a suite, override the spec field of DefaultRunnableSpec. For example:

1
2
3
object AllSuites extends DefaultRunnableSpec {
  def spec = suite("All tests")(suite1, suite2, suite3)
}

DefaultRunnableSpec provides an enviornment TestEnvironment to customize the system infrastructure.

To run the test, use zio.test.sbt.TestFramework or invoide the DefaultRunnableSpec.main method.

Test Environment

Test Aspects

Mocking Service