2014-01-28 10 views
0

我寫了一些ScalaTest Table-driven property checks,我試圖用sbt test運行它們。查看報告,我看到ScalaTest可以識別我所有的JUnit測試(它們與支票屬於同一班級),它運行屬性檢查(即forAll正文),但它不會將forAll作爲測試。如果失敗,我會在報告中看到堆棧跟蹤(使用ScalaTest失敗的測試例外)並且sbt表示在測試運行過程中出現「錯誤」,但它表示所有測試都通過了。報告中的測試總數僅包括JUnit測試。sbt不承認ScalaTest表驅動的屬性檢查作爲測試

sbt有沒有支持這種風格的測試?

回答

1

而不是調用forAll,使測試類從org.scalatest.prop.Checkers延伸,然後在每個測試中,調用check與屬性進行測試。在這種情況下,「屬性」可能意味着您創建的forAll

所以我要去猜測,目前你有一個測試類,看起來像:

class ExampleSuite extends AssertionsForJUnit { 
    val fractions = Table(
    ("n", "d"), 
    ( 1, 2), 
    ///... 
) 
    forAll (fractions) { (n: Int, d: Int) => // ... 

    @Test def verifySomethingElse = ??? 
} 

我相信你需要做的是從跳棋擴展和移動你的forAll到測試。

class ExampleSuite extends AssertionsForJUnit with org.scalatest.prop.Checkers { 
    @Test def verifyFractions = { 
    val fractions = Table(
     ("n", "d"), 
     ( 1, 2), 
     ///... 
    ) 
    check(forAll (fractions) { (n: Int, d: Int) => ???) 
    }  


    @Test def verifySomethingElse = ??? 
} 
5

forAll在PropertyChecks中不是測試。這本質上是一個榮耀的主張。您需要在命名測試中放置斷言。如何做到這一點取決於您選擇的風格。例如,在FunSuite,你會寫這樣的:

class MySpec extends FunSuite with PropertyChecks { 
    test("give the test a name here") { 
    forAll(x: Int, y: Int) { 
     // make assertions here 
    } 
    } 
} 
+0

你顯然是這裏的專家,所以我按照你的回答。 PropertyChecks是一種更簡單的方法來做'check(forAll ...)'的等價物嗎?我的回答是基於去年秋季在_Reactive Programming_課程中教授的方法。 –

+1

主要區別是PropertyChecks使用ScalaTest斷言或匹配表達式,而Checkers使用ScalaCheck的傳統布爾表達式。如果你的屬性變大了,你需要在你的布爾表達式中插入ScalaCheck標籤,我認爲它們比斷言或匹配表達式更難讀。 –

+0

@ Bill.Venners感謝您的解釋。我只是有機會使用PropertyChecks,並且必須說它比我一直使用的方法更清晰。 –

0

的標準方法是創建一個FunSuite測試與匹配器和TableDrivenPropertyCheck

例子:

import org.scalatest._ 
import org.scalatest.prop.TableDrivenPropertyChecks._ 

class CreateSpec extends FunSuite with Matchers { 

    test("B-Tree-Create for different degree parameter value") { 
    val params = Table(("degree", "result"), 
     (0, Tree(Leaf(), 0)), 
     (2, Tree(Leaf(), 1)), 
     (1999, Tree(Leaf(), 1999))) 

    forAll(params) {(degree, result) => Algorithms.create(degree) == result} 
    } 
}