如何在没有匹配器的情况下跳过specs2中的测试?

Ale*_*bon 7 scala skip specs2

我试图在scala中使用specs2测试一些db相关的东西.目标是测试"db running"然后执行测试.我发现如果数据库关闭,我可以使用Matcher类中的orSkip.

问题是,我正在获得一个匹配条件的输出(作为PASSED)并且示例被标记为SKIPPED.我想要的是:只有在测试数据库脱机时才执行一个标记为"SKIPPED"的测试.这是我的"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
  }
}
Run Code Online (Sandbox Code Playgroud)

这里是我的规范的代码:

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
  }
}
Run Code Online (Sandbox Code Playgroud)

现在出来:

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
Run Code Online (Sandbox Code Playgroud)

输出我希望它是:

Test MyClass should::do something(net.mycode.MyClassSpec) PASSED
Test MyClass should::do something with db(net.mycode.MyClassSpec) SKIPPED
Run Code Online (Sandbox Code Playgroud)

Eri*_*ric 6

我想你可以用一个简单的条件来做你想做的事:

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")
  }
}
Run Code Online (Sandbox Code Playgroud)


rle*_*ndi 5

你试过使用这个args(skipAll=true)论点吗?在这里几个例子.

不幸的是(据我所知),您不能跳过单位规范中的单个示例.但是,您可以使用此参数跳过规范结构,因此您可能必须创建单独的规范:

class MyClassSpec extends SpecificationWithJUnit {

  args(skipAll = false)

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

    "do something with db" in {
      success
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

  • 有专门的快捷方式,[`skipAllIf`](http://etorreborre.github.com/specs2/guide/org.specs2.guide.Structure.html#Skip+examples)跳过所有的例子,如果一个条件被满足.此方法较短,如果您的布尔表达式碰巧抛出任何异常,则会捕获异常. (3认同)