Authentication Test.QuickCheck

I want to extend QuickCheck to give me better messages when tests fail (and not just seed). For example, I would like to create things line by line:

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

or (with a Monoid instance that stops at SadResult and “continues” at HappyResult , akin to the statement (&&) for 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 xy <> listEqTest xs ys 

How can I extend the functionality of QuickCheck? Alternatively, is there a randomized testing library that is more extensible?

Thanks!

+4
source share
1 answer

Considering the QuickCheck documentation, the type you are looking for is 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 instance Testable Result you can use this as a return type of QuickCheck tests:

 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] 
+5
source

Source: https://habr.com/ru/post/1488972/


All Articles