我正在测试一个生成我自己类型实例的随机生成器。为此,我有一个 Arbitrary
的自定义实例:
complexGenerator :: (RandomGen g) => g -> (MyType, g)
instance Arbitrary MyType where
arbitrary = liftM (fst . complexGenerator . mkStdGen) arbitrary
这适用于 Test.QuickCheck
(实际上是 Test.Framework
),用于测试生成的值是否具有某些属性。但是,我要检查的属性相当多,添加的越多,验证它们所花费的时间就越多。
有没有办法使用相同的生成值来测试每个属性,而不是每次都重新生成它们?我显然仍然希望在失败时看到哪个属性不成立,因此使用和
制作一个巨大的属性并不是最佳选择。
最佳答案
I obviously still want to see, on failures, which property did not hold, so making one giant property with
and
is not optimal.
您可以使用 printTestCase
标记每个属性在用 conjoin
build 一个巨大的属性(property)之前.
例如你认为这是个坏主意:
prop_giant :: MyType -> Bool
prop_giant x = and [prop_one x, prop_two x, prop_three x]
这将同样高效,但会为您提供更好的输出:
prop_giant :: MyType -> Property
prop_giant x = conjoin [printTestCase "one" $ prop_one x,
printTestCase "two" $ prop_two x,
printTestCase "three" $ prop_three x]
(话虽如此,我自己从未使用过这种方法,只是假设它会起作用;conjoin
可能出于某种原因在文档中被标记为实验性的。)
关于testing - 测试.QuickCheck : speed up testing multiple properties for the same type,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15317041/