增强Test.QuickCheck

时间:2013-06-30 16:42:13

标签: haskell quickcheck

我希望扩展QuickCheck以便在测试失败时给出更好的消息(而不仅仅是种子)。例如,我希望能够按照以下方式创建:

eqTest :: Eq a => a -> a -> TestResult
eqTest x y = if x == y
             then HappyResult
             else SadResult $ show x <> " /= " <> show y

或(Monoid个实例在SadResult上“停止”并在HappyResult上“继续”,类似于(&&)的{​​{1}}运算符

TestResult

如何扩展QuickCheck功能?或者,是否有一个更具可扩展性的随机测试库?

谢谢!

1 个答案:

答案 0 :(得分:5)

通过阅读QuickCheck文档,您要查找的类型是Result

data Result
  = MkResult
  { ok          :: Maybe Bool     -- ^ result of the test case; Nothing = discard
  , expect      :: Bool           -- ^ indicates what the expected result of the property is
  , reason      :: String         -- ^ a message indicating what went wrong
  , interrupted :: Bool           -- ^ indicates if the test case was cancelled by pressing ^C
  , abort       :: Bool           -- ^ if True, the test should not be repeated
  , stamp       :: [(String,Int)] -- ^ the collected values for this test case
  , callbacks   :: [Callback]     -- ^ the callbacks for this test case
  }

并且感谢instance Testable Result您可以将其用作QuickCheck测试的返回类型:

Prelude Test.QuickCheck Test.QuickCheck.Property> let test xs = if 13 `elem` xs then MkResult (Just False) True "Input contained bad number" False False [] [] else succeeded
Prelude Test.QuickCheck Test.QuickCheck.Property> quickCheck test
*** Failed! Input contained bad number (after 17 tests and 3 shrinks):    
[13]