Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can i skip a test in specs2 without matchers?

Tags:

scala

skip

specs2

I am trying to test some db dependent stuff with specs2 in scala. The goal is to test for "db running" and then execute the test. I figured out that i can use orSkip from the Matcher class if the db is down.

The problem is, that i am getting output for the one matching condition (as PASSED) and the example is marked as SKIPPED. What i want instead: Only execute one test that is marked as "SKIPPED" in case the test db is offline. And here is the code for my "TestKit"

package net.mycode.testkit

import org.specs2.mutable._
import net.mycode.{DB}


trait MyTestKit {

  this: SpecificationWithJUnit =>

  def debug = false

  // Before example
  step {
    // Do something before
  }

  // Skip the example if DB is offline
  def checkDbIsRunning = DB.isRunning() must be_==(true).orSkip

  // After example
  step {
    // Do something after spec
  }
}

And here the code for my spec:

package net.mycode

import org.specs2.mutable._
import net.mycode.testkit.{TestKit}
import org.junit.runner.RunWith
import org.specs2.runner.JUnitRunner

@RunWith(classOf[JUnitRunner])
class MyClassSpec extends SpecificationWithJUnit with TestKit with Logging {

  "MyClass" should {
    "do something" in {
      val sut = new MyClass()
      sut.doIt must_== "OK"
    }

  "do something with db" in {
    checkDbIsRunning

    // Check only if db is running, SKIP id not
  }
}

Out now:

Test MyClass should::do something(net.mycode.MyClassSpec) PASSED
Test MyClass should::do something with db(net.mycode.MyClassSpec) SKIPPED
Test MyClass should::do something with db(net.mycode.MyClassSpec) PASSED

And output i want it to be:

Test MyClass should::do something(net.mycode.MyClassSpec) PASSED
Test MyClass should::do something with db(net.mycode.MyClassSpec) SKIPPED
like image 308
Alebon Avatar asked Jun 07 '12 09:06

Alebon


3 Answers

I think you can use a simple conditional to do what you want:

class MyClassSpec extends SpecificationWithJUnit with TestKit with Logging {

  "MyClass" should {
    "do something" in {
      val sut = new MyClass()
      sut.doIt must_== "OK"
    }
    if (DB.isRunning) {
      // add examples here
      "do something with db" in { ok }
    } else skipped("db is not running")
  }
}
like image 113
Eric Avatar answered Oct 20 '22 16:10

Eric


Have you tried using the args(skipAll=true) argument? See a few examples here.

Unfortunately (as far as I know), you cannot skip a single example in a unit specification. You can, however, skip the specification structure with this argument like this, so you might have to create separate specifications:

class MyClassSpec extends SpecificationWithJUnit {

  args(skipAll = false)

  "MyClass" should {
    "do something" in {
      success
    }

    "do something with db" in {
      success
    }
  }
}
like image 28
rlegendi Avatar answered Oct 20 '22 15:10

rlegendi


A new feature addressing this has been added to specs 2.3.10.

like image 42
Meredith Avatar answered Oct 20 '22 15:10

Meredith