In the specs2 framework, why does using Scope prevent the forAll quantifier from being executed?

In the code below, how can I get Specs2 to execute the first test? The print test passes when it does not work. The code inside the section is forAll()

not executed due to new Scope

.

Operators println

are for trace output only. Please let me know if you see lines starting with "one".

Empty Scope

is only intended to demonstrate the problem. This is removed from the code where I actually use variables in Scope

.

import org.scalacheck.Gen
import org.scalacheck.Prop._
import org.specs2.ScalaCheck
import org.specs2.mutable.Specification
import org.specs2.specification.Scope

class TestingSpec extends Specification with ScalaCheck {
  "sample test" should {
    "print ones" in new Scope { 
      println("The first test passes, but should fail")
      forAll(Gen.posNum[Int]) { (x: Int) =>
          println("one: " + x)
          (0 < x) mustNotEqual true
      } 
    } 

    "print twos" in { 
      println("The second test passes and prints twos")
      forAll(Gen.posNum[Int]) { (x: Int) =>
        println("two: " + x)
        (0 < x) mustEqual true
      } 
    } 
  }
}

      

Here's my output:

sbt> testOnly TestingSpec
The second test passes and prints twos
The first test passes, but should fail
two: 1
two: 2
two: 1
    ...
two: 50
two: 34
two: 41
[info] TestingSpec
[info] 
[info] sample test should
[info]   + print ones
[info]   + print twos
[info] 
[info] Total for specification TestingSpec
[info] Finished in 96 ms
[info] 2 examples, 101 expectations, 0 failure, 0 error
[info] 
[info] Passed: Total 2, Failed 0, Errors 0, Passed 2
[success] Total time: 3 s, completed Apr 28, 2015 3:14:15 PM

      

PS I have updated my project dependency from 2.4.15 to specs2 3.5. There is still this problem ...

+3


source to share


1 answer


This is because anything you put in Scope

should throw exceptions when there is a crash. ScalaCheck Prop

won't do anything if you haven't executed it so that your example will always pass.

A workaround is to convert Prop

to specs2 Result

using the following implicit, for example:

import org.specs2.execute._

implicit class RunProp(p: Prop) {
  def run: Result = 
    AsResult(p)
}

      



Then

"print ones" in new Scope { 
  println("The first test passes, but should fail")
   forAll(Gen.posNum[Int]) { (x: Int) =>
     println("one: " + x)
      (0 < x) mustNotEqual true
   }.run 
} 

      

+4


source







All Articles