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.1.0

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

lazyvalexample=project.in(file(".")).settings(scalaVersion:="3.4.2",libraryDependencies+="org.scala-lang"%%"toolkit-test"%"0.7.0"%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.1.0"%Test

In your build.sc file, you can add a test object extending Tests and TestModule.Munit:

objectexampleextendsScalaModule{defscalaVersion="3.4.2"objecttestextendsTestswithTestModule.Munit{defivyDeps=Agg(ivy"org.scala-lang::toolkit-test:0.7.0")}}

Alternatively, you can require just a specific version of MUnit:

ivy"org.scalameta::munit:1.1.0"

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"){valobtained=2+3valexpected=4assertEquals(obtained,expected)}
test("failing test"){valobtained=2+3valexpected=4assertEquals(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:

close