[英]Augmenting Test.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
,類似於一個(&&)
操作者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
如何擴展QuickCheck功能? 或者,是否有一個更具可擴展性的隨機測試庫?
謝謝!
從閱讀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]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.