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

2024-05-19

我正在尝试使用 scala 中的 specs2 测试一些与数据库相关的内容。目标是测试“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
  }
}

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

我希望输出是:

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

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

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")
  }
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

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

随机推荐