brander
brander

Reputation: 121

Random tests generated by Haskell HSpec property

I am running tests with Hspec and Quickcheck http://hspec.github.io/

The provided example to execute a random test case is

it "returns the first element of an *arbitrary* list" $
      property $ \x xs -> head (x:xs) == (x :: Int)

With associated output:

returns the first element of an *arbitrary* list 

How can I see the actual run-time values generated for the test? So given above example, sample desired output would include values passed for x and xs with something like:

returns the first element of an *arbitrary* list 
    \x xy head (x:xs) == (x :: Int) with x = 'a' and xs = "bc" holds 

Upvotes: 3

Views: 518

Answers (2)

Nicolas Henin
Nicolas Henin

Reputation: 3334

With the new version of HSpec (>= 2.5.0) you just have to write it this way:

it "returns the first element of an *arbitrary* list" $
  verbose $ \x xs -> head (x:xs) == (x :: Int)

Look at the discussion here: https://github.com/hspec/hspec/issues/257

Upvotes: 1

Zeta
Zeta

Reputation: 105915

No. Hspec's runner disables any QuickCheck output. Also, the random tests will provide a lot of noise. However, there's a workaround:

import Test.Hspec
import Test.QuickCheck
import Test.QuickCheck.Test (isSuccess)

verboseProperty :: Testable prop => prop -> Expectation
verboseProperty p = verboseCheckResult p >>= (`shouldBe` True) . isSuccess

main = hspec $ describe "head" $
  it "returns the first element of an *arbitrary* list" $
    verboseProperty $ \x xs -> head (x:xs) == (x :: Int)

However, the formatting will differ:

returns the first element of an *arbitrary* list
Passed:  
0
[]
Passed: 
1
[-1]
Passed:  
2
[-2]
Passed:  
3
[]
Passed:  
0
[]
Passed:  
1
[2,-5,5,5]
Passed:  
0
[-3,-1,-5,3]
…

There are of course more drawbacks, but that might be an easy way out. But passing tests aren't that interesting, it's the counter-examples that are more important—which are shown by default.

Upvotes: 1

Related Questions