id | title |
---|---|
filtering |
Filtering tests |
MUnit provides several options for selecting what tests to run.
Use testOnly -- $GLOB
to filter a fully qualified test name from the command
line.
# sbt shell
> testOnly -- *issue-456
> testOnly -- com.foo.controllers.*
Use .only
to run only a single test without custom command-line arguments.
object Tests extends munit.FunSuite
import Tests._
test("issue-457") {
// will not run
}
test("issue-456".only) {
// only test that runs
}
test("issue-455") {
// will not run
}
Use testOnly -- --tests=$REGEXP
to filter an individual test name from the
command line.
# sbt shell
> testOnly -- --tests=issue-456
It's necessary to quote the --tests=
flag if the test name contains spaces.
# sbt shell
> testOnly -- "--tests=test case with space"
Use .ignore
to skip an individual test case in a test suite.
test("issue-456".ignore) {
// will not run
}
Use assume(condition, explanation)
to skip tests when some conditions do not
hold. For example, use assume
to conditionally run tests based on the
operating system or the Scala compiler version.
import scala.util.Properties
test("paths") {
assume(Properties.isLinux, "this test runs only on Linux")
assume(Properties.versionNumberString.startsWith("2.13"), "this test runs only on Scala 2.13")
}
Use the @IgnoreSuite
annotation to skip all tests in a test suite.
@munit.IgnoreSuite
class MySuite extends munit.FunSuite {
test("hello1") {
// will not run
}
test("hello2") {
// will not run
}
// ...
}
The IgnoreSuite
annotation is only supported on the JVM. A workaround for
Scala.js and Scala Native is to mark the test suite as abstract so that it
doesn't run.
- @munit.IgnoreSuite
- class MySuite extends munit.FunSuite {
+ abstract class MySuite extends munit.FunSuite {
test("hello1") {
// will not run
Override munitIgnore: Boolean
to skip an entire test suite based on a dynamic
condition.
class MyWindowsOnlySuite extends munit.FunSuite {
override def munitIgnore: Boolean = !scala.util.Properties.isWin
test("windows-only") {
// only runs on Windows
}
}
Use --include-tags=$TAG1,$TAG2
and --exclude-tags=$TAG1,$TAG2
to include and
exclude which tests to run based on tags. For example, imagine you have the
following test suite:
class TagsSuite extends munit.FunSuite {
val include = new munit.Tag("include")
val exclude = new munit.Tag("exclude")
test("a".tag(include)) {}
test("b".tag(exclude)) {}
test("c".tag(include).tag(exclude)) {}
test("d") {}
}
Arguments | Matching tests |
---|---|
<no arguments> |
<all tests> |
--include-tags=include |
a, c |
--include-tags=include --exclude-tags=exclude |
a |
--exclude-tags=exclude |
a, d |
Configure the sbt setting testOptions.in(Test)
to automatically enable MUnit
flags when running myproject/test
. For example, use this to skip tests by
default that are tagged as slow
// build.sbt
val MUnitFramework = new TestFramework("munit.Framework")
lazy val myproject = project
.settings(
testFrameworks += MUnitFramework,
// ...
+ testOptions.in(Test) += Tests.Argument(MUnitFramework, "--exclude-tags=Slow")
)
You can use the same technique to create custom sbt tasks myproject/slow:test
and myproject/all:test
that run only slow tests and all tests, respectively.
// build.sbt
val MUnitFramework = new TestFramework("munit.Framework")
+ val Slow = config("slow").extend(Test)
+ val All = config("slow").extend(Test)
lazy val myproject = project
+ .configs(Slow, All)
.settings(
testFrameworks += MUnitFramework,
+ inConfig(Slow)(Defaults.testTasks),
+ inConfig(All)(Defaults.testTasks),
// ...
+ testOptions.in(All) := List(),
+ testOptions.in(Test) += Tests.Argument(MUnitFramework, "--exclude-tags=Slow"),
+ testOptions.in(Slow) -= Tests.Argument(MUnitFramework, "--exclude-tags=Slow"),
+ testOptions.in(Slow) += Tests.Argument(MUnitFramework, "--include-tags=Slow")
)
This feature is only supported on the JVM.
Use the @Category(...)
annotation from JUnit to group tests suites together.
// package myapp
import org.junit.experimental.categories.Category
class Slow extends munit.Tag("Slow")
class Fast extends munit.Tag("Fast")
@Category(Array(classOf[Slow]))
class MySlowSuite extends munit.FunSuite {
test("slow") {
Thread.sleep(1000)
}
// ...
}
@Category(Array(classOf[Slow], classOf[Fast]))
class MySlowFastSuite extends munit.FunSuite {
// ...
}
@Category(Array(classOf[Fast]))
class MyFastSuite extends munit.FunSuite {
// ...
}
Next, use --include-categories=$CATEGORY
and --exclude-categories=$CATEGORY
to
determine what test suites to run from the command line.
# matches: MySlowSuite, MySlowFastSuite
> testOnly -- --include-categories=myapp.Slow
# matches: MySlowSuite
> testOnly -- --include-categories=myapp.Slow --exclude-categories=myapp.Fast
Override munitTests()
to customize what tests get executed. For example, use
this feature to skip tests based on a dynamic condition.
import scala.util.Properties
case object Windows extends munit.Tag("Windows")
class MyWindowsTagSuite extends munit.FunSuite {
override def munitTests(): Seq[Test] = {
val default = super.munitTests()
if (!Properties.isWin) default
else default.filter(_.tags.contains(Windows))
}
test("files".tag(Windows)) {
// will always run, including on Windows
}
test("files") {
// will not run in Windows
}
}