You can use arbitrary QuickCheck properties with Hspec, but they must be of
type Property. QuickCheck's property function can be used to
turn anything that is a member of the Testable class into a
Property. Here is an example:
describe "read" $ do
it "is inverse to show" $ property $
\x -> (read . show) x `shouldBe` (x :: Int)
-- file Spec.hs
import Test.Hspec
import Test.QuickCheck
main :: IO ()
main = hspec $ do
describe "read" $ do
context "when used with Int" $ do
it "is inverse to show" $ property $
\x -> (read . show) x `shouldBe` (x :: Int)runhaskell Spec.hs
read
when used with Int
is inverse to show [✔]
+++ OK, passed 100 tests.
Finished in 0.0005 seconds
1 example, 0 failures
it ".." $ property $ .. is a common pattern. The
Test.Hspec.QuickCheck module provides the prop function as a
shortcut. With prop, the last example can be written as:
describe "read" $ do
prop "is inverse to show" $
\x -> (read . show) x `shouldBe` (x :: Int)
-- file Spec.hs
import Test.Hspec
import Test.Hspec.QuickCheck
main :: IO ()
main = hspec $ do
describe "read" $ do
context "when used with Int" $ do
prop "is inverse to show" $
\x -> (read . show) x `shouldBe` (x :: Int)runhaskell Spec.hs
read
when used with Int
is inverse to show [✔]
+++ OK, passed 100 tests.
Finished in 0.0005 seconds
1 example, 0 failures
It's also possible to modify some of the arguments passed to the Quickcheck driver, namely: the maximum number of successes before succeeding, the maximum number of discarded tests per successful test before giving up and the size of the test case:
import Test.Hspec.QuickCheck (modifyMaxSuccess)
describe "read" $ do
modifyMaxSuccess (const 1000) $ it "is inverse to show" $ property $
\x -> (read . show) x `shouldBe` (x :: Int)