The Scala Toolkit

How to run a single test?

Language

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"

Running a single test suite

To run a single example.MyTests suite with Scala CLI, use the --test-only option of the test command.

scala-cli test example --test-only example.MyTests

To run a single example.MyTests suite in sbt, use the testOnly task:

sbt:example> testOnly example.MyTests

To run a single example.MyTests suite in Mill, use the testOnly task:

./mill example.test.testOnly example.MyTests

Running a single test in a test suite

Within a test suite file, you can select individual tests to run by temporarily appending .only, e.g.

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)
  }

In the above example, only the "multiplication" tests will run (i.e. "addition" is ignored). This is useful to quickly debug a specific test in a suite.

Alternative: excluding specific tests

You can exclude specific tests from running by appending .ignore to the test name. For example the following ignores the "addition" test, and run all the others:

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)
  }

Use tags to group tests, and run specific tags

MUnit lets you group and run tests across suites by tags, which are textual labels. The MUnit docs have instructions on how to do this.

Contributors to this page: