8

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
Alebon
  • 1,189
  • 2
  • 11
  • 24

3 Answers3

7

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")
  }
}
Eric
  • 15,494
  • 38
  • 61
  • It's not perfect at all but very helpful ;) – Alebon Jun 16 '12 at 12:44
  • @Eric This throws exception- Uncaught exception when running test.functionality.Login: org.specs2.execute.SkipException. Is there a way where this would not throw exception? – 0fnt May 12 '15 at 05:32
  • 1
    I think that `"db is not running" in skipped` instead should work. – Eric May 12 '15 at 05:38
  • And just to be 100% it doesn't have to extend the `SpecificationWithJunit` - [the `skipped` result](https://etorreborre.github.io/specs2/guide/SPECS2-3.8.9/org.specs2.guide.StandardResults.html#skipping-an-example) for an example is part of the main specs2 `StandardResult` – rbellamy Mar 30 '17 at 19:48
6

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
    }
  }
}
rlegendi
  • 10,466
  • 3
  • 38
  • 50
  • btw you don't need @RunWith when inheriting from WithJUnit – OlegYch Jun 07 '12 at 15:12
  • That's right, fixed that. BTW I'm working on the [Scala IDE integration of Specs2 during GSoC 2012](http://xcafebabe.blogspot.hu/2012/06/first-thoughts-on-scala.html), hopefully we'll be able to run it without any annotation at the end of the summer :-) – rlegendi Jun 07 '12 at 15:44
  • 3
    There is a dedicated shortcut, [`skipAllIf`](http://etorreborre.github.com/specs2/guide/org.specs2.guide.Structure.html#Skip+examples) to skip all the examples if a condition is satisfied. This method is shorter and will catch exceptions if your boolean expression happens to throw any. – Eric Jun 07 '12 at 22:04
  • @Eric skipAllIf doesn't exist anymore, it seems – Meredith Dec 17 '13 at 22:31
2

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

Meredith
  • 3,928
  • 4
  • 33
  • 58