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 scala.util.Random.alphanumeric
val randomString = IO(alphanumeric.take(10).mkString(""))
loggedTest("failure should print logs") { log =>
for {
currentTime <- IO.realTime.map(_.toSeconds)
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:
repl.MdocSessionMdocAppMySuite + logging for success 11ms repl.MdocSessionMdocAppMyAnotherSuite - failure should print logs 10ms *************FAILURES************* repl.MdocSessionMdocAppMyAnotherSuite - failure should print logs 10ms
assertion failed (multiple_suites_logging.md:41)
[INFO] 13:44:42 [multiple_suites_logging.md:38] Starting the test...
time -> 1731073482
purpose -> docs
[DEBUG] 13:44:42 [multiple_suites_logging.md:40] Generated random string: YFzkZhnfyP Total 2, Failed 1, Passed 1, Ignored 0, Cancelled 0