Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
962 views
in Technique[技术] by (71.8m points)

scala - How to programmatically ignore/skip tests with ScalaTest?

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...