[英]Property Based Testing with Exceptions
I am writing an introduction to Haskell for my local functional programming group. 我正在为我的本地功能编程小组撰写Haskell的简介。 As a base I am using the Tasty-testing framework and I want to test the indexing function
(!!)
. 作为基础,我正在使用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])
]
The tests should not fail on "too large indices" as the Errors/Exceptions are the same. 试验不应在“过大索引”的错误失败/异常是相同的。
The tests should fail with negative input - which could be resolved by adding NonNegative
as a constraint on the input, or adding the respective clause in the myIndex
-function. 测试应该在输入为负的情况下失败-可通过在输入上添加
NonNegative
作为约束或在myIndex
函数中添加相应的子句来解决。
您可以使用spoon包,或者编写类似的函数,如果您想测试异常是否相同,将返回异常。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.