我想用specs2在scala中测试一些数据库相关的东西。目标是测试“db运行”,然后执行测试。我发现如果数据库关闭,我可以使用Matcher类中的orSkip。如何在没有匹配器的情况下跳过specs2中的测试?
问题是,我得到了一个匹配条件的输出(如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
你可以举一个例子,说明当前的控制台输出是什么,以及期望的输出是什么? – Eric
添加输出样本 – Alebon