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 13ms
repl.MdocSessionMdocAppMyAnotherSuite
- failure should print logs 17ms
*************FAILURES*************
repl.MdocSessionMdocAppMyAnotherSuite
- failure should print logs 17ms
assertion failed (multiple_suites_logging.md:41)
} yield expect(x.length > 20)
| | |
| 10 false
lsLdiJHuJE
[INFO] 09:32:00 [multiple_suites_logging.md:38] Starting the test...
time -> 1668936720
purpose -> docs
[DEBUG] 09:32:00 [multiple_suites_logging.md:40] Generated random string: lsLdiJHuJE
Total 2, Failed 1, Passed 1, Ignored 0, Cancelled 0