我想用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