简体   繁体   English

增强Test.QuickCheck

[英]Augmenting Test.QuickCheck

I want to extend QuickCheck to give me better messages when tests fail (rather than just the seed). 我想扩展QuickCheck以在测试失败时给出更好的消息(而不仅仅是种子)。 For instance, I'd like to be able to create things along the lines of: 例如,我希望能够创建以下内容:

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

or (with a Monoid instance which "stops" on SadResult and "continues" on HappyResult , akin to a (&&) operator for TestResult ) 或(具有Monoid实例在其“停止” SadResult和“继续”上HappyResult ,类似于一个(&&)操作者TestResult

listEqTest :: Eq a => [a] -> [a] -> TestResult
listEqTest [] [] = HappyResult
listEqTest [] ys = SadResult $ "Ran out of xs to compare to " <> show ys
listEqTest xs [] = SadResult $ "Ran out of ys to compare to " <> show xs
listEqTest (x:xs) (y:ys) = eqTest x y <> listEqTest xs ys

How can I go about extending the QuickCheck functionality? 如何扩展QuickCheck功能? Alternately, is there a randomized testing library which is more extensible? 或者,是否有一个更具可扩展性的随机测试库?

Thanks! 谢谢!

From reading the QuickCheck documentation, the type you are looking for is Result : 从阅读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
  }

and thanks to the instance Testable Result you can use this as the return type of QuickCheck tests: 并且由于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]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM