Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to programmatically ignore/skip tests with ScalaTest?

Tags:

I am running some tests with ScalaTest which rely on connections to test servers to be present. I currently created my own Spec similar to this:

abstract class ServerDependingSpec extends FlatSpec with Matchers {    def serverIsAvailable: Boolean = {     // Check if the server is available   } } 

Is it possible to ignore (but not fail) tests when this method returns false?

Currently I do it in a "hackish" way:

"Something" should "do something" in {   if(serverIsAvailable) {     // my test code   } } 

but I want something like

whenServerAvailable "Something" should "do something" in {    // test code } 

or

"Something" should "do something" whenServerAvailable {    // test code } 

I think I should define my custom tag, but I can only refer to the source code of in or ignore and I do not understand how I should plug in my custom implementations.

How should I accomplish this?

like image 763
rabejens Avatar asked Sep 08 '15 14:09

rabejens


People also ask

How do you ignore test cases in ScalaTest?

It has been inserted into Scaladoc by pretending it is a trait. When you mark a test class with a tag annotation, ScalaTest will mark each test defined in that class with that tag. Thus, marking the SetSpec in the above example with the @Ignore tag annotation means that both tests in the class will be ignored.

What is ScalaTest used for?

Overview. ScalaTest is one of the most popular, complete and easy-to-use testing frameworks in the Scala ecosystem. Where ScalaTest differs from other testing tools is its ability to support a number of different testing styles such as XUnit and BDD out of the box.


1 Answers

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 } 
like image 51
Holger Brandl Avatar answered Sep 20 '22 07:09

Holger Brandl