您可以在單一行中要求整個工具包
//> using toolkit latest
MUnit 是一個測試架構,僅在測試檔案中可用:test
目錄中的檔案或具有 .test.scala
延伸名的檔案。請參閱 Scala CLI 文件 以深入了解測試範圍。
或者,您只需要求特定版本的 MUnit
//> using dep org.scalameta::munit:1.0.0-M7
在您的 build.sbt 檔案中,您可以新增對 toolkit-test 的依賴關係
lazy val example = project.in(file("example"))
.settings(
scalaVersion := "3.2.2",
libraryDependencies += "org.scala-lang" %% "toolkit-test" % "0.1.7" % Test
)
這裡的 Test
設定表示此相依性僅由 example/src/test
中的原始檔使用。
或者,您只需要求特定版本的 MUnit
libraryDependencies += "org.scalameta" %% "munit" % "1.0.0-M7" % Test
在 build.sc 檔中,您可以新增一個 test
物件,延伸 Tests
和 TestModule.Munit
object example extends ScalaModule {
def scalaVersion = "3.2.2"
object test extends Tests with TestModule.Munit {
def ivyDeps =
Agg(
ivy"org.scala-lang::toolkit-test:0.1.7"
)
}
}
或者,您只需要求特定版本的 MUnit
ivy"org.scalameta::munit:1.0.0-M7"
加入線索以取得更好的錯誤報告
在 assert
內使用 clue
,可在斷言失敗時取得更好的錯誤報告。
assert(clue(List(a).head) > clue(b))
// munit.FailException: assertion failed
// Clues {
// List(a).head: Int = 1
// b: Int = 2
// }
在 MUnit 文件 中深入了解線索。
撰寫特定於環境的測試
使用 assume
來撰寫特定於環境的測試。 assume
可以包含布林條件。您可以檢查作業系統、Java 版本、Java 屬性、環境變數或其他任何項目。如果測試的其中一項假設未達成,則會略過測試。
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/"))
}
在 MUnit 文件 中深入了解過濾測試。
標記不穩定的測試
您可以使用 flaky
標記測試,以標示為不穩定。透過將 MUNIT_FLAKY_OK
環境變數設定為 true
,可以略過不穩定的測試。
test("requests".flaky) {
// I/O heavy tests that sometimes fail
}
test("requests".flaky) {
// I/O heavy tests that sometimes fail
}
在 MUnit 文件 中深入了解不穩定的測試