Logging
Weaver only outputs the logs for tests that failed - the logs are neatly collected alongside the failure information and reported after all the tests have run.
Additionally, each log can have a context associated with it - which gets printed alongside the message.
import weaver._
import cats.effect._
object MySuite extends SimpleIOSuite {
val randomUUID = IO(java.util.UUID.randomUUID())
loggedTest("logging for success") { log =>
for {
x <- randomUUID
y <- randomUUID
_ <- log.info(s"Generated $x and $y")
} yield expect(x != y)
}
}
object MyAnotherSuite extends SimpleIOSuite {
import java.util.concurrent.TimeUnit
import scala.util.Random.alphanumeric
val randomString = IO(alphanumeric.take(10).mkString(""))
loggedTest("failure should print logs") { log =>
for {
currentTime <- timer.clock.realTime(TimeUnit.SECONDS)
context = Map("time" -> currentTime.toString, "purpose" -> "docs")
_ <- log.info("Starting the test...", context)
x <- randomString
_ <- log.debug(s"Generated random string: $x")
} yield expect(x.length > 20)
}
}
The report would look something like this:
MySuite
+ logging for success 13ms
MyAnotherSuite
- failure should print logs 17ms
*************FAILURES*************
MyAnotherSuite
- failure should print logs 17ms
assertion failed (multiple_suites_logging.md:42)
} yield expect(x.length > 20)
| | |
| 10 false
aA2LVVsPEZ
[INFO] 16:03:20 [multiple_suites_logging.md:39] Starting the test...
time -> 1618239800
purpose -> docs
[DEBUG] 16:03:20 [multiple_suites_logging.md:41] Generated random string: aA2LVVsPEZ
Total 2, Failed 1, Passed 1, Ignored 0, Cancelled 0