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