How to programmatically ignore/skip tests with ScalaTest?
Here is some trick to skip a test based on a condition:
object WhenServerAvailable extends Tag(if (serverIsAvailable) "" else classOf[Ignore].getName)
"Something" should "do something" taggedAs WhenServerAvailable in { ... }
You can use Tags to achieve this:
Documentation on how to use Tags : http://www.scalatest.org/user_guide/tagging_your_tests
Adding and removing tagged test with command line parameters: http://www.scalatest.org/user_guide/using_the_runner#specifyingTagsToIncludeAndExclude
Example Code:
import org.scalatest.Tag
object ServerIsAvailable extends Tag("biz.neumann.ServerIsAvailable")
"Something" should "do something" taggedAs(ServerIsAvailable) in {
// your test here
}
Running the tests
Running the tests is a bitt tricky. It only works for testOnly and testQuick not test. In the example testOnly is short for testOnly *
sbt "testOnly -- -l biz.neumann.ServerAvailable"
You can use assume
to conditionally cancel a test:
"Something" should "do something" in {
assume(serverIsAvailable)
// my test code
}
or you can test for some condition yourself, and then use cancel
:
"Something" should "do something" in {
if(!serverIsAvailable) {
cancel
}
// my test code
}