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$1$React.react(Framework.scala:953)
at org.scalatest.tools.Framework$ScalaTestRunner$Skeleton$1.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$1.apply(DispatchReporter.scala:240)
at org.scalatest.DispatchReporter$Propagator$$anonfun$run$1.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$1.apply(DispatchReporter.scala:240)
at org.scalatest.DispatchReporter$Propagator$$anonfun$run$1.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$1.apply(DispatchReporter.scala:240)
at org.scalatest.DispatchReporter$Propagator$$anonfun$run$1.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$1.apply(DispatchReporter.scala:240)
at org.scalatest.DispatchReporter$Propagator$$anonfun$run$1.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$1.apply(DispatchReporter.scala:240)
at org.scalatest.DispatchReporter$Propagator$$anonfun$run$1.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$2.apply(DispatchReporter.scala:251)
at org.scalatest.DispatchReporter$Propagator$$anonfun$run$2.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 не содержат полезной информации.
1 ответ
Ваш тест и код, кажется, в порядке.
Вы, наверное, уже это знаете, но в любом случае ваш первый тест просто проваливается (пока не обращайте внимания на странные исключения).
Исправление первого теста, заменяя его следующим:
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 - это две отдельные тестовые среды, хотя вы используете некоторые функции ScalaCheck внутри ScalaTest. Но sbt не знает об этом, и, поскольку вы включили обе платформы, он ищет два вида тестов и сообщает результаты для каждой среды.
Вот почему отчет ScalaCheck печатается с 0 выполненными тестами. После этого тесты ScalaTest запускаются, и вы получаете отчет. Третий и последний отчет от самого sbt, суммирующий результаты всех тестовых сред.
Возвращаясь к странным исключениям, они появятся только в том случае, если ваше тестирование не пройдено, так что вам не следует слишком о них беспокоиться. Но если это действительно раздражает вас, добавьте это в свой build.sbt
:
fork in Test := false
И странные исключения исчезнут.