加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

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

发布时间:2020-12-16 19:19:50 所属栏目:安全 来源:网络整理
导读:我试图在 scala中使用specs2测试一些db依赖的东西.目标是测试“db running”然后执行测试.我发现如果数据库关闭,我可以使用Matcher类中的orSkip. 问题是,我正在获得一个匹配条件的输出(作为PASSED),并且示例被标记为SKIPPED.我想要的是:只有在测试数据库脱
我试图在 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
  }
}

这里是我的规范的代码:

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读