您可以在單一程式碼行中要求整個工具組
//> 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"
執行單一測試套件
若要使用 Scala CLI 執行單一 example.MyTests
套件,請使用 test
指令的 --test-only
選項。
scala-cli test example --test-only example.MyTests
若要在 sbt 中執行單一 example.MyTests
套件,請使用 testOnly
任務
sbt:example> example/testOnly example.MyTests
若要在 Mill 中執行單一 example.MyTests
套件,請使用 testOnly
任務
./mill example.test.testOnly example.MyTests
在測試套件中執行單一測試
在測試套件檔案中,您可以透過暫時附加 .only
來選擇要執行的個別測試,例如
class MathSuite extends munit.FunSuite {
test("addition") {
assert(1 + 1 == 2)
}
test("multiplication".only) {
assert(3 * 7 == 21)
}
}
class MathSuite extends munit.FunSuite:
test("addition") {
assert(1 + 1 == 2)
}
test("multiplication".only) {
assert(3 * 7 == 21)
}
在上述範例中,只有 "multiplication"
測試會執行 (亦即 "addition"
會被忽略)。這對於在套件中快速偵錯特定測試很有用。
替代方案:排除特定測試
您可以透過附加 .ignore
到測試名稱來排除特定測試。例如,以下會忽略 "addition"
測試,並執行所有其他測試
class MathSuite extends munit.FunSuite {
test("addition".ignore) {
assert(1 + 1 == 2)
}
test("multiplication") {
assert(3 * 7 == 21)
}
test("remainder") {
assert(13 % 5 == 3)
}
}
class MathSuite extends munit.FunSuite:
test("addition".ignore) {
assert(1 + 1 == 2)
}
test("multiplication") {
assert(3 * 7 == 21)
}
test("remainder") {
assert(13 % 5 == 3)
}
使用標籤來群組測試,並執行特定標籤
MUnit 讓你可以透過標籤(文字標籤)來群組和執行跨套件的測試。MUnit 文件 有關於如何這樣做的說明。