如何在没有匹配器的情况下跳过 specs2 中的测试? [英] How can i skip a test in specs2 without matchers?

查看:49
本文介绍了如何在没有匹配器的情况下跳过 specs2 中的测试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

问题是,我正在获取一个匹配条件的输出(如 PASSED),并且示例被标记为 SKIPPED.我想要的是:如果测试数据库离线,只执行一个标记为SKIPPED"的测试.这是我的TestKit"的代码

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

现在:

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

推荐答案

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

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

这篇关于如何在没有匹配器的情况下跳过 specs2 中的测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆