8

我正在尝试在 scala 中使用 specs2 测试一些依赖于 db 的东西。目标是测试“db running”,然后执行测试。我发现如果数据库关闭,我可以使用 Matcher 类中的 orSkip 。

问题是,我正在获得一个匹配条件的输出(作为通过),并且该示例被标记为跳过。我想要的是:只执行一个标记为“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
  }
}

这里是我的规范的代码:

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
  }
}

现在出来:

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

输出我希望它是:

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

3 回答 3

7

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

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")
  }
}
于 2012-06-11T03:47:12.690 回答
5

您是否尝试过使用该args(skipAll=true)参数?请参阅此处的几个示例

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

class MyClassSpec extends SpecificationWithJUnit {

  args(skipAll = false)

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

    "do something with db" in {
      success
    }
  }
}
于 2012-06-07T12:06:13.137 回答
2

解决此问题的新功能已添加到规范 2.3.10中。

于 2014-03-21T17:46:28.860 回答