考虑Haskell中的以下QuickCheck程序
{-# LANGUAGE TemplateHaskell #-}
import Test.QuickCheck
import Test.QuickCheck.All
prop_trivial :: Bool
prop_trivial = 42 == (6 * 7)
-- Wacky boilerplate to make all tests run.
return []
runTests = $quickCheckAll
main = do
runTests
这适用于ghc版本7.8.3和QuickCheck 2.7.6。问题是它重复测试100次。我四处寻找缓解措施,并在the QuickCheck docs here中找到exhaustive
。 Groovy的!我将prop_trivial
更改为以下内容:
prop_trivial = exhaustive $ property $ 42 == (6 * 7)
进行类型检查和编译,但失败了:
=== prop_trivial from /blahblahblah/FooTest.hs:6 ===
*** Failed! Falsifiable (after 1 test):
False
我对如何理解和调试此结果感到有点困惑;文档对我来说太薄了,无法弄清楚发生了什么。
答案 0 :(得分:3)
您可能会喜欢smallcheck。它不像QuickCheck那样发达,但对于详尽的测试非常好。在QuickCheck生成随机输入的地方,smallcheck系统地生成其输入,在所有输入上运行属性,直到给定(由您)大小。对于人口数量有限的类型,这使得设置详尽的测试变得非常简单,并且因为我们不会尝试生成随机的东西,所以它也很容易避免重复测试。这是一个简单的例子:
Test.SmallCheck> smallCheck 10 (6*7 == 42)
Completed 1 tests without failure.
10
是用于选择何时停止生成输入的最大深度;由于没有输入,因此在此示例中忽略了它。有关使用大小的示例,请考虑:
Test.SmallCheck> smallCheck 10 (\x -> x <= (10 :: Integer))
Completed 21 tests without failure.
Test.SmallCheck> smallCheck 20 (\x -> x <= (10 :: Integer))
Failed test no. 22.
there exists 11 such that
condition is false
答案 1 :(得分:2)
您似乎可以使用once
修改属性以仅运行一次。
一些示例代码:
{-# LANGUAGE TemplateHaskell #-}
import Test.QuickCheck
import Test.QuickCheck.All
prop_trivial :: Int -> Bool
prop_trivial x = x == x
prop_trivial2 = once prop_trivial
prop_true = True
prop_true2 = once True
-- Wacky boilerplate to make all tests run.
return []
runTests = $quickCheckAll
main = do
putStrLn $ "exhaustive prop_trivial = " ++ show (exhaustive prop_trivial)
putStrLn $ "exhaustive prop_trivial2 = " ++ show (exhaustive prop_trivial2)
putStrLn $ "exhaustive prop_true = " ++ show (exhaustive prop_true)
putStrLn $ "exhaustive prop_true2 = " ++ show (exhaustive prop_true2)
runTests
输出:
exhaustive prop_trivial = False
exhaustive prop_trivial2 = False
exhaustive prop_true = True
exhaustive prop_true2 = False
=== prop_trivial from qc2.hs:5 ===
+++ OK, passed 100 tests.
=== prop_trivial2 from qc2.hs:7 ===
+++ OK, passed 1 tests.
=== prop_true from qc2.hs:8 ===
+++ OK, passed 100 tests.
=== prop_true2 from qc2.hs:9 ===
+++ OK, passed 1 tests.
exhaustive
prop 只有在知道 prop 的测试是详尽无遗的情况下才会返回True - 请注意exhaustive True
和{{1}之间的区别}。
答案 2 :(得分:0)
source非常简单。 exhaustive
的默认定义用于Property
实例,默认值为exhaustive _ = False
。我想你可能想要exhaustive $ 42 == (6 * 7)