The Scala Toolkit

How to run tests?

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.0-M7

In your build.sbt file, you can add the dependency on toolkit-test:

lazy val example = project.in(file("."))
  .settings(
    scalaVersion := "3.3.3",
    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.0-M7" % 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.3"
  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.0-M7"

Running the tests

You can run all of your test suites with a single command.

Using Scala CLI, the following command runs all the tests in the folder example:

scala-cli test example
# Compiling project (test, Scala 3.2.1, JVM)
# Compiled project (test, Scala 3.2.1, JVM)
# MyTests:
#  + sum of two integers 0.009s

In the sbt shell, the following command runs all the tests of the project example:

sbt:example> test
# MyTests:
#   + sum of two integers 0.006s
# [info] Passed: Total 1, Failed 0, Errors 0, Passed 1
# [success] Total time: 0 s, completed Nov 11, 2022 12:54:08 PM

In Mill, the following command runs all the tests of the module example:

./mill example.test.test
# [71/71] example.test.test
# MyTests:
#   + sum of two integers 0.008s

The test report, printed in the console, shows the status of each test. The + symbol before a test name shows that the test passed successfully.

Add and run a failing test to see how a failure looks:

test("failing test") {
  val obtained = 2 + 3
  val expected = 4
  assertEquals(obtained, expected)
}
test("failing test") {
  val obtained = 2 + 3
  val expected = 4
  assertEquals(obtained, expected)
}
# MyTests:
#   + sum of two integers 0.008s
# ==> X MyTests.failing test  0.015s munit.ComparisonFailException: ./MyTests.test.scala:13
# 12:    val expected = 4
# 13:    assertEquals(obtained, expected)
# 14:  }
# values are not the same
# => Obtained
# 5
# => Diff (- obtained, + expected)
# -5
# +4
#     at munit.Assertions.failComparison(Assertions.scala:274)

The line starting with ==> X indicates that the test named failing test fails. The following lines show where and how it failed. Here it shows that the obtained value is 5, where 4 was expected.

Contributors to this page: