繁体   English   中英

基于属性的异常测试

[英]Property Based Testing with Exceptions

我正在为我的本地功能编程小组撰写Haskell的简介。 作为基础,我正在使用Delicious测试框架,并且我想测试索引功能(!!)

MinimalExample.hs

module MinimalExample where

myIndex :: Int -> [a] -> a                                               
myIndex _ [] = error "index too large"                                   
myIndex 0 (x:_) = x                                                      
myIndex n (_:xs) = myIndex (n-1) xs                                      

MinimalTests.hs

module MinimalTests where

import Test.Tasty
import Test.Tasty.SmallCheck as SC
import Test.SmallCheck.Series

import MinimalExample

main :: IO ()
main = defaultMain tests

tests :: TestTree
tests = testGroup "Tests" [scProps]

scProps ::  TestTree
scProps = testGroup "(checked by SmallCheck)"
  [ SC.testProperty "(!!) == myIndex" $ \lst n ->
      lst !! n == myIndex (n::Int) (lst::[Int])
  ]

试验不应在“过大索引”的错误失败/异常是相同的。

测试应该在输入为负的情况下失败-可通过在输入上添加NonNegative作为约束或在myIndex函数中添加相应的子句来解决。

您可以使用spoon包,或者编写类似的函数,如果您想测试异常是否相同,将返回异常。

暂无
暂无

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

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