Search code examples
specs2

specs2 skip test based on condition without an exception


Is there a way to skip a test in specs2 without the SkippedException occurring? I'd like the test to be ignored under a certain condition so that it doesn't show as error but as ignored. For example: I have the following in the test/scala/Skipped.scala file

package models

import org.specs2.mutable._
import org.specs2.runner._

class Skipped extends Specification{
  "Application" should {
    if( 3 < 2 ){
      "do better with regex" in {

        "axbcd" must find( "bc".r )
      }
    }
    else skipped( "blah" )
  }
}

and the following the my build.sbt

scalaVersion := "2.10.3"

libraryDependencies += "org.specs2" % "specs2_2.10" % "2.1.1" % "test"

When I run the test, I get the following:

org.specs2.execute.SkipException: blah
at org.specs2.matcher.ThrownExpectations$class.skipped(ThrownExpectations.scala:87)

Am I missing something?

Bonus question: Why am I using such an old version? When I have the following in my build.sbt, I cannot compile the above code snippet.

scalaVersion := "2.11.1"

libraryDependencies += "org.specs2" %% "specs2" % "2.3.12" % "test"

The error is:

Skipped.scala:7: overloaded method value should with alternatives:
[error]   (fs: => Unit)(implicit p1: Skipped.this.ImplicitParam1, implicit p2: Skipped.this.ImplicitParam2)org.specs2.specification.Fragments <and>
[error]   (fs: => org.specs2.specification.Fragments)(implicit p: Skipped.this.ImplicitParam)org.specs2.specification.Fragments <and>
[error]   (fs: => org.specs2.specification.Fragment)org.specs2.specification.Fragments
[error]  cannot be applied to (Product with Serializable)
[error]   "Application" should {
[error]                 ^
[error] one error found

So I'd appreciate if someone could help me with a way to skip a test without an exception, which preferably works in with the newer versions of specs2


Solution

  • This should work:

    class Skipped extends Specification{
       "Application" should {
         if( 3 < 2 ){
           "do better with regex" in {
             "axbcd" must find( "bc".r )
           }
         }
         else "3 is >= 2" in skipped( "blah" )
       }
    }
    

    The difference is that skipped is declared in the body of an example which allows it to be reported to the console:

    [info] o 3 is >= 2
    [info]    SKIPPED blah
    

    (or something like that, I didn't run the code)