2

scalatest.FlatSpec テスト ファイルで scalacheck プロパティ ジェネレーターを使用しようとしています。

テストは失敗し、junit フレームワーク (および私の場合は eclipse) によって報告されるはずですが、テスト パスとエラーはコンソールに表示されるだけです。

import scala.collection.immutable.TreeSet
import org.junit.runner.RunWith
import org.raisercostin.namek.UnitSpec
import org.scalatest.junit.JUnitRunner
import org.scalatest.FlatSpec
import org.scalatest._

@RunWith(classOf[JUnitRunner])
class SetsTest2 extends FlatSpec with Matchers 
     with OptionValues with Inside with Inspectors {
  import org.scalacheck.Prop
  "set intersection" should "be commutative" in {
    Prop.forAll { (l1: TreeSet[Int], l2: TreeSet[Int]) =>
      l1.intersect(l2) == l1.intersect(l1)
    }.check
  }
}

出力は次のとおりです

Run starting. Expected test count is: 1
SetsTest2:
set intersection

! Falsified after 1 passed tests.
> ARG_0: TreeSet(0)
> ARG_0_ORIGINAL: TreeSet(1288089760)
> ARG_1: TreeSet()
> ARG_1_ORIGINAL: TreeSet(0)
- should be commutative
Run completed in 505 milliseconds.
Total number of tests run: 1
Suites: completed 1, aborted 0
Tests: succeeded 1, failed 0, canceled 0, ignored 0, pending 0
All tests passed.

エラーがjunitフレームワークにバブルアップすることを期待していました。

次の依存関係があります。

scalaVersion    = "2.10.4"
"junit" % "junit" % "4.10" % "test"
"org.scalatest" %% "scalatest" % "2.2.4" % "test"
"org.scalacheck" %% "scalacheck" % "1.12.2" % "test"
4

2 に答える 2

2

scalacheck.P​​rop.check とは異なる scalatest.prop.Checkers を使用する必要があります。

import scala.collection.immutable.TreeSet
import org.junit.runner.RunWith
import org.raisercostin.namek.UnitSpec
import org.scalatest.junit.JUnitRunner
import org.scalatest.FlatSpec
import org.scalatest._
import org.scalatest.prop.Checkers

@RunWith(classOf[JUnitRunner])
class SetsTest2 extends FlatSpec with Matchers 
      with OptionValues with Inside with Inspectors with Checkers {
  import org.scalacheck.Prop
  "set intersection" should "be commutative" in {
    check(Prop.forAll { (l1: TreeSet[Int], l2: TreeSet[Int]) =>
      l1.intersect(l2) == l1.intersect(l1)
    })
  }
}

出力は次のようになります

Run starting. Expected test count is: 1
SetsTest2:
set intersection
- should be commutative *** FAILED ***
  GeneratorDrivenPropertyCheckFailedException was thrown during property evaluation.
   (SetsTest.scala:17)
    Falsified after 1 successful property evaluations.
    Location: (SetsTest.scala:17)
    Occurred when passed generated values (
      arg0 = TreeSet(0), // 1 shrink
      arg1 = TreeSet() // 1 shrink
    )
Run completed in 452 milliseconds.
Total number of tests run: 1
Suites: completed 1, aborted 0
Tests: succeeded 0, failed 1, canceled 0, ignored 0, pending 0
*** 1 TEST FAILED ***
于 2015-03-15T10:13:49.220 に答える
0

多くの人にとっては、 raisercostin の答えで十分です。ただし、ScalaCheck と ScalaTest の最新バージョンが完全に統合されていないという問題をいくつか見てきました。新しい機能が必要かもしれません。

ただし、sbt のようなツールを使用する利点の 1 つは、両方を並べて実行できることです。これは最善の方法ではないかもしれませんが、FlatSpec テストを 1 つのファイルに、ScalaCheck Props を別のファイルに入れることができSetsTest2ますSetsProps2

次に、 を実行するsbt testと、すべてのテストが実行され、正しく返されるはずです! 確認するために、33 の FlatSpec テストと 2 つの ScalaCheck Prop を含む小さなアプリケーションで意図的に偽の ScalaCheck Prop を実行し、

[info] ScalaTest
[info] Run completed in 2 seconds, 211 milliseconds.
[info] Total number of tests run: 33
[info] Suites: completed 8, aborted 0
[info] Tests: succeeded 33, failed 0, canceled 0, ignored 0, pending 0
[info] All tests passed.
[error] Failed: Total 35, Failed 1, Errors 0, Passed 34
[error] Failed tests:
[error]         com.xxx.xxx.TestProps
[error] (test:test) sbt.TestsFailedException: Tests unsuccessful 
于 2016-04-12T15:57:41.387 に答える