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:
repl.MdocSessionMdocAppMyAnotherSuite
- failure should print logs 10ms
repl.MdocSessionMdocAppMySuite
+ logging for success 26ms
*************FAILURES*************
repl.MdocSessionMdocAppMyAnotherSuite
- failure should print logs 10ms
assertion failed (multiple_suites_logging.md:42)
} yield expect(x.length > 20)
| | |
| 10 false
FunHtONyWb
[INFO] 13:24:32 [multiple_suites_logging.md:39] Starting the test...
time -> 1660137872
purpose -> docs
[DEBUG] 13:24:32 [multiple_suites_logging.md:41] Generated random string: FunHtONyWb
Total 2, Failed 1, Passed 1, Ignored 0, Cancelled 0