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

标签 scala skip specs2

我正在尝试使用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
  }
}

现已推出:

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

关于scala - 在没有匹配器的情况下如何跳过specs2中的测试?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10928839/

相关文章:

scala - 如何在 Scala 中获取动态创建类的完整类名

Perl 调试器 - 如何跳出循环

java - 如何使用 FTP 协议(protocol)恢复读取输入流

c# - 跳过数组中的最后一个元素并返回所有其他元素 C#

scala - 测试重定向后加载的页面

scala - 在 Specs2 中使用 Akka TestKit

scala - Specs2 - "should not equal to"- 如何检查不相等?

scala - 如何使用第一个 map 中的键和合并值将两个 map 合并成一个?

scala - IntelliIdea - 在添加框架支持中看不到 scala

scala - 解析 JSON 和选项[任何]