How to get ScalaTest to report test results correctly when using scalacheck with Propspec and PropertyCheck?

I would like to test my scala program using property-based testing with scalacheck. I wrote:

class MyProperties extends PropSpec with PropertyChecks { property("My property") { val myProperty: org.scalacheck.Prop = new MyProperty // some code I need to set myProperty myProperty.check } } 

But this seems wrong, because when I run this class using ScalaTest, I got to the console:

 Run starting. Expected test count is: 1 MyProperties: ! Falsified after 51 passed tests. > ARG_0: myGeneratedArgument - My property Run completed in 1 second, 623 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. 

So, the problem is that my property is falsified, but the test passes!?! Does anyone see what is wrong with my code?

Thanks...

EDIT: I tried calling myProperty instead of myProperty.check, but this is not much better, as the generators seem to be ignored (only one test is run instead of a hundred).

+6
source share
2 answers

In the end, I found a way to write my test, which is taken into account by Scalatest. I used the Checkers property instead of PropertyChecks:

 class MyProperties extends PropSpec with Checkers { property("My property") { val myProperty: org.scalacheck.Prop = new MyProperty // some code I need to set myProperty Checkers.check(myProperty) } } 

I'm not sure if this is the best way to write this, but I get what I wanted. Locally:

 *** FAILED *** GeneratorDrivenPropertyCheckFailedException was thrown during property evaluation. (MyProperties.scala:175) Falsified after 0 successful property evaluations. Location: (MyProperties.scala:175) Occurred when passed generated values ( arg0 = myGeneratedArgument ) 

and finally:

 Run completed in 4 seconds, 514 milliseconds. Total number of tests run: 1 Suites: completed 1, aborted 0 Tests: succeeded 0, failed 1, canceled 0, ignored 0, pending 0 *** 1 TESTS FAILED *** 

If someone could appreciate this offer, I would be happy ^^

+2
source

From ScalaTest, you need to either use Checkers or PropertyChecks. If you use the traditional ScalaCheck properties, and it looks like you are, you should use Checkers (as you discovered). The only thing I would add is that you can just say check instead of Checkers.check:

 class MyProperties extends PropSpec with Checkers { property("My property") { val myProperty: org.scalacheck.Prop = new MyProperty // some code I need to set myProperty check(myProperty) } } 

Full documentation for Checkers is here:

http://doc.scalatest.org/2.0/index.html#org.scalatest.prop.Checkers

+1
source

Source: https://habr.com/ru/post/958296/


All Articles