You can require the entire toolkit in a single line:
//> using toolkit latest
MUnit, being a testing framework, is only available in test files: files in a test
directory or ones that have the .test.scala
extension. Refer to the Scala CLI documentation to learn more about the test scope.
Alternatively, you can require just a specific version of MUnit:
//> using dep org.scalameta::munit:1.0.3
In your build.sbt file, you can add the dependency on toolkit-test:
lazy val example = project.in(file("."))
.settings(
scalaVersion := "3.3.4",
libraryDependencies += "org.scala-lang" %% "toolkit-test" % "0.1.7" % Test
)
Here the Test
configuration means that the dependency is only used by the source files in src/test
.
Alternatively, you can require just a specific version of MUnit:
libraryDependencies += "org.scalameta" %% "munit" % "1.0.3" % Test
In your build.sc file, you can add a test
object extending Tests
and TestModule.Munit
:
object example extends ScalaModule {
def scalaVersion = "3.3.4"
object test extends Tests with TestModule.Munit {
def ivyDeps =
Agg(
ivy"org.scala-lang::toolkit-test:0.1.7"
)
}
}
Alternatively, you can require just a specific version of MUnit:
ivy"org.scalameta::munit:1.0.3"
Adding clues to get better error report
Use clue
inside an assert
to a get a better error report when the assertion fails.
assert(clue(List(a).head) > clue(b))
// munit.FailException: assertion failed
// Clues {
// List(a).head: Int = 1
// b: Int = 2
// }
Learn more about clues in the MUnit documentation.
Writing environment-specific tests
Use assume
to write environment-specific tests.
assume
can contain a boolean condition. You can check the operating system, the Java version, a Java property, an environment variable, or anything else.
A test is skipped if one of its assumptions isn’t met.
import scala.util.Properties
test("home directory") {
assume(Properties.isLinux, "this test runs only on Linux")
assert(os.home.toString.startsWith("/home/"))
}
import scala.util.Properties
test("home directory") {
assume(Properties.isLinux, "this test runs only on Linux")
assert(os.home.toString.startsWith("/home/"))
}
Learn more about filtering tests in the MUnit documentation.
Tagging flaky tests
You can tag a test with flaky
to mark it as being flaky.
Flaky tests can be skipped by setting the MUNIT_FLAKY_OK
environment variable to true
.
test("requests".flaky) {
// I/O heavy tests that sometimes fail
}
test("requests".flaky) {
// I/O heavy tests that sometimes fail
}
Learn more about flaky tests in the MUnit documentation