您如何在Haskell QuickCheck中生成一个随机实例,然后对该实例进行进一步的测试?
让我举个例子来演示。在下面的代码片段中,我生成了一个Int
,然后想要对该Int
进行进一步的测试:
{-# OPTIONS -Wall #-}
import Test.Tasty
import Test.Tasty.QuickCheck as QC
import qualified Debug.Trace as Trace
main :: IO()
main = defaultMain tests
test1 :: Int -> Property
test1 i = withMaxSuccess 2 (test2 i)
test2 :: Int -> (Int, Int) -> Property
test2 i (a, b) =
Trace.trace ("(n,a,b) = " ++ show (i, a, b)) $
property True
tests :: TestTree
tests =
testGroup
"Test suite"
[
QC.testProperty "Test" (withMaxSuccess 3 test1)
]
作为输出,我希望有类似的东西:
(n,a,b) = (0,0,0)
(n,a,b) = (0,1,2)
(n,a,b) = (-1,-1,-1)
(n,a,b) = (-1,-2,-3)
(n,a,b) = (2,0,-2)
(n,a,b) = (2,1,-1)
但是我得到:
(n,a,b) = (0,0,0)
(n,a,b) = (-1,-1,-1)
(n,a,b) = (2,0,-2)
我找到了这个post (How can QuickCheck test all properties for each sample),但并没有真正帮助我。
考虑withMaxSuccess
功能:
withMaxSuccess :: Testable prop => Int -> prop -> Property
这是一个将任何Testable
转换为Property
的功能。结果Property
的行为是它将以Int
指定的最大数量运行。
test1
函数是一个Testable
实例。调用withMaxSuccess 3 test1
时,将更改test1
的行为,以使运行时最多运行3次。
test1
返回仅运行两次(Property
)的行为的withMaxSuccess 2
,却被withMaxSuccess 3
覆盖。
withMaxSuccess
函数不会运行测试,它只会更改属性的定义。 withMaxSuccess 3
是该属性的最新修改,因此这就是您看到它运行三次的原因。