使用 quickCheckAll 的完整最小示例

Complete minimal example for using quickCheckAll

我可以使用一个完整的示例来说明如何使用 quickCheckAll。到目前为止,这是我尝试过的方法:

在文件中 A.hs:

module A where
    import Test.QuickCheck

    prop_a = 1 == 0

    check = do
        return []
        $quickCheckAll

在另一个应该驱动测试的文件中:

import A

main :: IO ()
main = do
    check

这不起作用,因为支票没有类型 IO ()。我应该如何按照指示 "execute check" in the documentation?

我觉得你有点误读 the documentation。它指定您应该将 return [] 写为裸表达式,并且您必须使用 TemplateHaskell:

Test all properties in the current module, using Template Haskell. You need to have a {-# LANGUAGE TemplateHaskell #-} pragma in your module for any of these to work.

(...)

To use quickCheckAll, add a definition to your module along the lines of

return []
runTests = $quickCheckAll

and then execute runTests.

(...)

Note: the bizarre return [] in the example above is needed on GHC 7.8; without it, quickCheckAll will not be able to find any of the properties. For the curious, the return [] is a Template Haskell splice that makes GHC insert the empty list of declarations at that point in the program; GHC typechecks everything before the return [] before it starts on the rest of the module, which means that the later call to quickCheckAll can see everything that was defined before the return []. Yikes!

所以你的第一个文件应该是:

<b>{-# LANGUAGE TemplateHaskell #-}</b>

module A where

import Test.QuickCheck

prop_a = 1 == 0

<b>return []
check = $quickCheckAll</b>

您要测试的所有属性(此处 prop_a)应在 return [].

之前定义

和主文件:

import A

main :: IO <b>Bool</b>
main = <b>check</b>

您可以使用 do,但它不会增加任何价值。或者,如果您希望 mainIO (),您可以这样写:

import A

main :: IO ()
main = <b>do
    check
    return ()</b>

运行 ghci 中的这个给出:

*A> :t check
check :: IO Bool
*A> check
=== prop_a from ha.hs:7 ===
*** Failed! Falsifiable (after 1 test):  

False