How to set persistent seeds for the Haskell quickCheck function

Every time I run "quickCheck prop_xyz", a new random seed is used. How to make QuickCheck always use the same random seed?

Thanks!

+4
source share
4 answers

Necessary functions Test.QuickCheck:; use quickCheckWithto specify custom Args. In particular, there is a field replay :: Maybe (StdGen, Int)that allows you to repeat the tests. Thus, you can use the stdArgsdefault values ​​and configure them; eg,

ghci> :load Main.hs
ghci> import Test.QuickCheck
ghci> import System.Random -- for mkStdGen
ghci> quickCheckWith stdArgs{replay = Just (mkStdGen 42, 0)} prop_xyz

The second component of the tuple is related to the size of the test cases, but I definitely remember that.

+5

quickCheckWith

quickCheckWith (stdArgs{replay = Just (myNewGen, testSize)}) property

, , ,

result <- quickCheckResult failing_prop
let gen = usedSeed result
let size = usedSize result

, .

, . [], , () .

+3

Generator, , :

import Test.QuickCheck
import System.Random


reproducableTest :: Testable a => a -> Maybe (StdGen, Int) -> IO ()
reproducableTest prop repl = do result <- quickCheckWithResult stdArgs{replay = repl} prop
                                case result of
                                     Failure{interrupted = False} -> do putStrLn $ "Use " ++ show (usedSeed result) ++ " as the initial seed"
                                                                        putStrLn $ "Use " ++ show (usedSize result) ++ " as the initial size"
                                     _       -> return ()

. , prop, reproducableTest prop Nothing. -

  Use x y as the initial seed
  Use z as the initial size

reproducableTest prop $ Just (mkStdGen x , z)

0

QuickCheck >= 2.7 tf-random.

If the result of your failure test run looks something like this:

Failure {
    usedSeed = TFGenR 1CE4E8B15F9197B60EE70803C62388671B62D6F88720288F5339F7EC521FEBC4 0 70368744177663 46 0,
    USEDSIZE = 75,
    ...        
}

Then you can reproduce the failure as follows:

import Test.QuickCheck.Random (QCGen(..))
import System.Random.TF (TFGen)

qcheck :: Testable a => a -> IO ()
qcheck prop = quickCheckWith args prop
    where
        usedSeed = QCGen (read "TFGenR 1CE4E8B15F9197B60EE70803C62388671B62D6F88720288F5339F7EC521FEBC4 0 70368744177663 46 0"::TFGen)
        usedSize = 75
        args = stdArgs{
            replay=Just(usedSeed, usedSize)
        }
0
source

All Articles