assume
утверждение, указанное внутри объекта контекста фикстуры , может использоваться для реализации семантики условного игнорирования, зависящей от флага среды. Например, рассмотрим следующее IfIgnored
приспособление
trait IfIgnored extends Assertions {
assume(System.getenv("runIgnoredTest").toBoolean, "!!! TEST IGNORED !!!")
}
который может быть создан, например, так:
it should "not say goodbye" in new IfIgnored {
Hello.greeting shouldNot be ("goodbye")
}
Теперь, если мы определим следующие настройки в build.sbt
Test / fork := true,
Test / envVars := Map("runIgnoredTest" -> "false")
и следующие тесты
class HelloSpec extends FlatSpec with Matchers {
"The Hello object" should "say hello" in {
Hello.greeting shouldEqual "hello"
}
it should "not say goodbye" in new IfIgnored {
Hello.greeting shouldNot be ("goodbye")
}
it should "not say live long and prosper" in new IfIgnored {
Hello.greeting shouldNot be ("live long and prosper")
}
}
затем выполнение sbt test
должно вывести
[info] HelloSpec:
[info] The Hello object
[info] - should say hello
[info] - should not say goodbye !!! CANCELED !!!
[info] scala.Predef.augmentString(java.lang.System.getenv("runIgnoredTest")).toBoolean was false !!! TEST IGNORED !!! (HelloSpec.scala:6)
[info] - should not say live long and prosper !!! CANCELED !!!
[info] scala.Predef.augmentString(java.lang.System.getenv("runIgnoredTest")).toBoolean was false !!! TEST IGNORED !!! (HelloSpec.scala:6)
[info] Run completed in 2 seconds, 389 milliseconds.
[info] Total number of tests run: 1
[info] Suites: completed 1, aborted 0
[info] Tests: succeeded 1, failed 0, canceled 2, ignored 0, pending 0
[info] All tests passed.
, где мы видим только should say hello
бег, а остальные были проигнорированы.
Чтобы выполнить только игнорируемые тесты, мы можем определить следующую пользовательскую команду testOnlyIgnored
:
commands += Command.command("testOnlyIgnored") { state =>
val ignoredTests = List(
""""should not say goodbye"""",
""""should not say live long and prosper""""
).mkString("-z ", " -z ", "")
"""set Test / envVars := Map("runIgnoredTest" -> "true")""" ::
s"""testOnly -- $ignoredTests""" :: state
}
Обратите внимание, как мы используем аргумент -z
runner для запуска определенных тестов, например,
testOnly -- -z "should not say goodbye" -z "should not say live long and prosper"
Также обратите внимание, как мы вручную добавляем имена тестов в ignoredTests
. Теперь выполнение sbt testOnlyIgnored
должно вывести
[info] HelloSpec:
[info] The Hello object
[info] - should not say goodbye
[info] - should not say live long and prosper
[info] Run completed in 2 seconds, 298 milliseconds.
[info] Total number of tests run: 2
[info] Suites: completed 1, aborted 0
[info] Tests: succeeded 2, failed 0, canceled 0, ignored 0, pending 0
[info] All tests passed.
там, где мы видим, should say hello
не был запущен, в то время как все игнорируемые тесты были выполнены.
Если мы откажемся от необходимости запускать игнорируемые тесты отдельно, тогда мы можем использовать предоставленную аннотацию ignore
, например,
ignore should "not say goodbye" in {
Hello.greeting shouldNot be ("goodbye")
}
который на sbt test
выводит
[info] HelloSpec:
[info] The Hello object
[info] - should say hello
[info] - should not say goodbye !!! IGNORED !!!
[info] - should not say live long and prosper !!! IGNORED !!!
[info] Run completed in 2 seconds, 750 milliseconds.
[info] Total number of tests run: 1
[info] Suites: completed 1, aborted 0
[info] Tests: succeeded 1, failed 0, canceled 0, ignored 2, pending 0