PlayFramework + ScalaTest + ScalaCheck

PlayFramework + ScalaTest + ScalaCheck

我正在使用标题中提到的框架,配置如下:

"com.typesafe.play" % "sbt-plugin" % "2.4.2"

"org.scalacheck" %% "scalacheck" % "1.12.4" % "test"
"org.scalatest" %% "scalatest" % "2.2.5" % "test"
"org.scalatestplus" %% "play" % "1.4.0-M4" % "test"

以简单的测试为目的进行演示:

class User
        extends FlatSpec
        with Matchers
        with Checkers {
    it should "do scala check stuff" in {
        check( ( a: Int ) ⇒ a > 100 )
    }

    it should "do scala check stuff II" in {
        check( ( a: Int ) ⇒ true )
    }
}

它似乎基本上可以工作,但是 sbt test 输出中充满了异常。

[project-id] $ test
[info] User:
[info] p.a.l.c.ActorSystemProvider - Starting application default Akka system: application
[info] p.a.l.c.ActorSystemProvider - Shutdown application default Akka system: application
[info] - should do scala check stuff *** FAILED ***
[info]   GeneratorDrivenPropertyCheckFailedException was thrown during property evaluation.
[info]    (User.scala:157)
[info]     Falsified after 0 successful property evaluations.
[info]     Location: (User.scala:157)
[info]     Occurred when passed generated values (
[info]       arg0 = -1
[info]     )
Exception in thread "Thread-31" java.lang.ClassCastException: cannot assign instance of scala.collection.immutable.List$SerializationProxy to field org.scalacheck.Test$Failed.args of type scala.collection.immutable.List in instance of org.scalacheck.Test$Failed
    at java.io.ObjectStreamClass$FieldReflector.setObjFieldValues(ObjectStreamClass.java:2089)
    at java.io.ObjectStreamClass.setObjFieldValues(ObjectStreamClass.java:1261)
    at java.io.ObjectInputStream.defaultReadFields(ObjectInputStream.java:1999)
    at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1918)
    at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1801)
    at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1351)
    at java.io.ObjectInputStream.defaultReadFields(ObjectInputStream.java:1993)
    at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1918)
    at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1801)
    at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1351)
    at java.io.ObjectInputStream.defaultReadFields(ObjectInputStream.java:1993)
    at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1918)
    at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1801)
    at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1351)
    at java.io.ObjectInputStream.defaultReadFields(ObjectInputStream.java:1993)
    at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1918)
    at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1801)
    at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1351)
    at java.io.ObjectInputStream.defaultReadFields(ObjectInputStream.java:1993)
    at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1918)
    at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1801)
    at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1351)
    at java.io.ObjectInputStream.defaultReadFields(ObjectInputStream.java:1993)
    at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1918)
    at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1801)
    at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1351)
    at java.io.ObjectInputStream.defaultReadFields(ObjectInputStream.java:1993)
    at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1918)
    at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1801)
    at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1351)
    at java.io.ObjectInputStream.readObject(ObjectInputStream.java:371)
    at org.scalatest.tools.Framework$ScalaTestRunner$Skeleton$React.react(Framework.scala:953)
    at org.scalatest.tools.Framework$ScalaTestRunner$Skeleton.run(Framework.scala:942)
    at java.lang.Thread.run(Thread.java:745)
Reporter completed abruptly with an exception after receiving event: TestFailed(Ordinal(0, 3),GeneratorDrivenPropertyCheckFailedException was thrown during property evaluation.
 (User.scala:157)
  Falsified after 0 successful property evaluations.
  Location: (User.scala:157)
  Occurred when passed generated values (
    arg0 = -1
  ),User,test.api.controller.User,Some(test.api.controller.User),should do scala check stuff,should do scala check stuff,Vector(),Some(org.scalatest.exceptions.GeneratorDrivenPropertyCheckFailedException: GeneratorDrivenPropertyCheckFailedException was thrown during property evaluation.
 (User.scala:157)
  Falsified after 0 successful property evaluations.
  Location: (User.scala:157)
  Occurred when passed generated values (
    arg0 = -1
  )),Some(3210),Some(IndentedText(- should do scala check stuff,should do scala check stuff,0)),Some(SeeStackDepthException),Some(test.api.controller.User),None,pool-1-thread-1-ScalaTest-running-User,1440662150684).
java.net.SocketException: Broken pipe
    at java.net.SocketOutputStream.socketWrite0(Native Method)
    at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:109)
    at java.net.SocketOutputStream.write(SocketOutputStream.java:153)
    at java.io.ObjectOutputStream$BlockDataOutputStream.drain(ObjectOutputStream.java:1877)
    at java.io.ObjectOutputStream$BlockDataOutputStream.setBlockDataMode(ObjectOutputStream.java:1786)
    at java.io.ObjectOutputStream.writeNonProxyDesc(ObjectOutputStream.java:1286)
    at java.io.ObjectOutputStream.writeClassDesc(ObjectOutputStream.java:1231)
    at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1427)
    at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1178)
    at java.io.ObjectOutputStream.writeFatalException(ObjectOutputStream.java:1577)
    at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:351)
    at org.scalatest.tools.SocketReporter.apply(SocketReporter.scala:31)
    at org.scalatest.DispatchReporter$Propagator$$anonfun$run.apply(DispatchReporter.scala:240)
    at org.scalatest.DispatchReporter$Propagator$$anonfun$run.apply(DispatchReporter.scala:239)
    at scala.collection.immutable.List.foreach(List.scala:381)
    at org.scalatest.DispatchReporter$Propagator.run(DispatchReporter.scala:239)
    at java.lang.Thread.run(Thread.java:745)
Reporter completed abruptly with an exception after receiving event: TestStarting(Ordinal(0, 4),User,test.api.controller.User,Some(test.api.controller.User),should do scala check stuff II,should do scala check stuff II,Some(MotionToSuppress),Some(LineInFile(160,User.scala)),Some(test.api.controller.User),None,pool-1-thread-1-ScalaTest-running-User,1440662150704).
java.net.SocketException: Broken pipe
    at java.net.SocketOutputStream.socketWrite0(Native Method)
    at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:109)
    at java.net.SocketOutputStream.write(SocketOutputStream.java:153)
    at java.io.ObjectOutputStream$BlockDataOutputStream.drain(ObjectOutputStream.java:1877)
    at java.io.ObjectOutputStream$BlockDataOutputStream.setBlockDataMode(ObjectOutputStream.java:1786)
    at java.io.ObjectOutputStream.writeNonProxyDesc(ObjectOutputStream.java:1286)
    at java.io.ObjectOutputStream.writeClassDesc(ObjectOutputStream.java:1231)
    at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1427)
    at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1178)
    at java.io.ObjectOutputStream.writeFatalException(ObjectOutputStream.java:1577)
    at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:351)
    at org.scalatest.tools.SocketReporter.apply(SocketReporter.scala:31)
    at org.scalatest.DispatchReporter$Propagator$$anonfun$run.apply(DispatchReporter.scala:240)
    at org.scalatest.DispatchReporter$Propagator$$anonfun$run.apply(DispatchReporter.scala:239)
    at scala.collection.immutable.List.foreach(List.scala:381)
    at org.scalatest.DispatchReporter$Propagator.run(DispatchReporter.scala:239)
    at java.lang.Thread.run(Thread.java:745)
[info] p.a.l.c.ActorSystemProvider - Starting application default Akka system: application
[info] p.a.l.c.ActorSystemProvider - Shutdown application default Akka system: application
[info] - should do scala check stuff II
Reporter completed abruptly with an exception after receiving event: TestSucceeded(Ordinal(0, 5),User,test.api.controller.User,Some(test.api.controller.User),should do scala check stuff II,should do scala check stuff II,Vector(),Some(506),Some(IndentedText(- should do scala check stuff II,should do scala check stuff II,0)),Some(LineInFile(160,User.scala)),Some(test.api.controller.User),None,pool-1-thread-1-ScalaTest-running-User,1440662151215).
java.net.SocketException: Broken pipe
    at java.net.SocketOutputStream.socketWrite0(Native Method)
    at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:109)
    at java.net.SocketOutputStream.write(SocketOutputStream.java:153)
    at java.io.ObjectOutputStream$BlockDataOutputStream.drain(ObjectOutputStream.java:1877)
    at java.io.ObjectOutputStream$BlockDataOutputStream.setBlockDataMode(ObjectOutputStream.java:1786)
    at java.io.ObjectOutputStream.writeNonProxyDesc(ObjectOutputStream.java:1286)
    at java.io.ObjectOutputStream.writeClassDesc(ObjectOutputStream.java:1231)
    at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1427)
    at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1178)
    at java.io.ObjectOutputStream.writeFatalException(ObjectOutputStream.java:1577)
    at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:351)
    at org.scalatest.tools.SocketReporter.apply(SocketReporter.scala:31)
    at org.scalatest.DispatchReporter$Propagator$$anonfun$run.apply(DispatchReporter.scala:240)
    at org.scalatest.DispatchReporter$Propagator$$anonfun$run.apply(DispatchReporter.scala:239)
    at scala.collection.immutable.List.foreach(List.scala:381)
    at org.scalatest.DispatchReporter$Propagator.run(DispatchReporter.scala:239)
    at java.lang.Thread.run(Thread.java:745)
Reporter completed abruptly with an exception after receiving event: SuiteCompleted(Ordinal(0, 6),User,test.api.controller.User,Some(test.api.controller.User),Some(3850),Some(MotionToSuppress),Some(TopOfClass(test.api.controller.User)),None,None,pool-1-thread-1,1440662151227).
java.net.SocketException: Broken pipe
    at java.net.SocketOutputStream.socketWrite0(Native Method)
    at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:109)
    at java.net.SocketOutputStream.write(SocketOutputStream.java:153)
    at java.io.ObjectOutputStream$BlockDataOutputStream.drain(ObjectOutputStream.java:1877)
    at java.io.ObjectOutputStream$BlockDataOutputStream.writeByte(ObjectOutputStream.java:1915)
    at java.io.ObjectOutputStream.writeFatalException(ObjectOutputStream.java:1576)
    at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:351)
    at org.scalatest.tools.SocketReporter.apply(SocketReporter.scala:31)
    at org.scalatest.DispatchReporter$Propagator$$anonfun$run.apply(DispatchReporter.scala:240)
    at org.scalatest.DispatchReporter$Propagator$$anonfun$run.apply(DispatchReporter.scala:239)
    at scala.collection.immutable.List.foreach(List.scala:381)
    at org.scalatest.DispatchReporter$Propagator.run(DispatchReporter.scala:239)
    at java.lang.Thread.run(Thread.java:745)
Reporter completed abruptly with an exception after receiving event: RunCompleted(Ordinal(0, 7),Some(4099),Some(Summary(1,1,0,0,0,1,0,0)),None,None,None,main,1440662151252).
java.net.SocketException: Broken pipe
    at java.net.SocketOutputStream.socketWrite0(Native Method)
    at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:109)
    at java.net.SocketOutputStream.write(SocketOutputStream.java:153)
    at java.io.ObjectOutputStream$BlockDataOutputStream.drain(ObjectOutputStream.java:1877)
    at java.io.ObjectOutputStream$BlockDataOutputStream.writeByte(ObjectOutputStream.java:1915)
    at java.io.ObjectOutputStream.writeFatalException(ObjectOutputStream.java:1576)
    at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:351)
    at org.scalatest.tools.SocketReporter.apply(SocketReporter.scala:31)
    at org.scalatest.DispatchReporter$Propagator$$anonfun$run.apply(DispatchReporter.scala:240)
    at org.scalatest.DispatchReporter$Propagator$$anonfun$run.apply(DispatchReporter.scala:239)
    at scala.collection.immutable.List.foreach(List.scala:381)
    at org.scalatest.DispatchReporter$Propagator.run(DispatchReporter.scala:239)
    at java.lang.Thread.run(Thread.java:745)
Reporter completed abruptly with an exception on invocation of the dispose method.
java.net.SocketException: Broken pipe
    at java.net.SocketOutputStream.socketWrite0(Native Method)
    at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:109)
    at java.net.SocketOutputStream.write(SocketOutputStream.java:153)
    at java.io.ObjectOutputStream$BlockDataOutputStream.drain(ObjectOutputStream.java:1877)
    at java.io.ObjectOutputStream$BlockDataOutputStream.flush(ObjectOutputStream.java:1822)
    at java.io.ObjectOutputStream.flush(ObjectOutputStream.java:719)
    at org.scalatest.tools.SocketReporter.dispose(SocketReporter.scala:37)
    at org.scalatest.DispatchReporter$.propagateDispose(DispatchReporter.scala:312)
    at org.scalatest.DispatchReporter$Propagator$$anonfun$run.apply(DispatchReporter.scala:251)
    at org.scalatest.DispatchReporter$Propagator$$anonfun$run.apply(DispatchReporter.scala:250)
    at scala.collection.immutable.List.foreach(List.scala:381)
    at org.scalatest.DispatchReporter$Propagator.run(DispatchReporter.scala:250)
    at java.lang.Thread.run(Thread.java:745)
[info] ScalaCheck
[info] Passed: Total 0, Failed 0, Errors 0, Passed 0
[info] ScalaTest
[info] Run completed in 4 seconds, 976 milliseconds.
[info] Total number of tests run: 0
[info] Suites: completed 0, aborted 0
[info] Tests: succeeded 0, failed 0, canceled 0, ignored 0, pending 0
[info] No tests were executed.
[error] Failed: Total 2, Failed 1, Errors 0, Passed 1
[error] Failed tests:
[error]     test.api.controller.User
[error] (api/test:test) sbt.TestsFailedException: Tests unsuccessful
[error] Total time: 6 s, completed 27.08.2015 09:55:51

我也很困惑,日志末尾有3个测试摘要,其中2个没有任何有用信息。

您的测试和代码似乎没问题。

您可能已经知道这一点,但无论如何,您的第一个测试只是失败了(暂时不要介意奇怪的异常)。

修复第一个测试,将其替换为:

check( ( a: Int ) ⇒ a / 1 == a )

我们得到:

[info] User:
[info] - should do scala check stuff (1 second, 421 milliseconds)
[info] - should do scala check stuff II (28 milliseconds)
[info] ScalaCheck
[info] Passed: Total 0, Failed 0, Errors 0, Passed 0
[info] ScalaTest
[info] Run completed in 12 seconds, 132 milliseconds.
[info] Total number of tests run: 2
[info] Suites: completed 1, aborted 0
[info] Tests: succeeded 2, failed 0, canceled 0, ignored 0, pending 0
[info] All tests passed.
[info] Passed: Total 2, Failed 0, Errors 0, Passed 2
[success] Total time: 23 s, completed Feb 14, 2016 12:44:44 AM

那 3 个测试摘要呢?

ScalaCheck 和 ScalaTest 是两个独立的测试框架,尽管您在 ScalaTest 中使用了 ScalaCheck 的一些功能。但 sbt 并不知道这一点,因为你包含了这两个框架,它会寻找两种测试,并报告每个框架的结果。

这就是为什么 ScalaCheck 的报告打印有 0 个运行的测试。在此之后,运行 ScalaTest 的测试并获得报告。第三个也是最后一个报告来自 sbt 本身,总结了所有测试框架的结果。

回到奇怪的异常,它们只会在您的测试失败时出现,所以您不必太担心它们。但如果它真的让你很烦,请将其添加到你的 build.sbt:

fork in Test := false

奇怪的异常将消失。