[英]Haskell: Multiple Assertions in Unit Testing?
I looked up a similar topic and found this snippet of code from here: https://stackoverflow.com/a/21419654/14386048我查找了一个类似的主题,并从这里找到了这段代码: https://stackoverflow.com/a/21419654/14386048
To quote:去引用:
Let's assume that we have module SafePrelude.hs
:假设我们有模块
SafePrelude.hs
:
module SafePrelude where
safeHead :: [a] -> Maybe a
safeHead [] = Nothing
safeHead (x:_) = Just x
we can put tests into TestSafePrelude.hs
as follows:我们可以将测试放入
TestSafePrelude.hs
,如下所示:
module TestSafePrelude where
import Test.HUnit
import SafePrelude
testSafeHeadForEmptyList :: Test
testSafeHeadForEmptyList =
TestCase $ assertEqual "Should return Nothing for empty list"
Nothing (safeHead ([]::[Int]))
testSafeHeadForNonEmptyList :: Test
testSafeHeadForNonEmptyList =
TestCase $ assertEqual "Should return (Just head) for non empty list" (Just 1)
(safeHead ([1]::[Int]))
main :: IO Counts
main = runTestTT $ TestList [testSafeHeadForEmptyList, testSafeHeadForNonEmptyList]
-- End Quote -- -- 结束报价 --
Is it possible to have multiple assertions within, let's say, testSafeHeadForEmptyList
?是否可以在
testSafeHeadForEmptyList
中有多个断言? I'm trying to categorize my test samples of similar cases, but just different variations.我正在尝试对类似案例的测试样本进行分类,但只是不同的变体。 I want to avoid something like this below (assume each assert equal has a different variation, but all should return nothing, for example):
我想避免下面这样的事情(假设每个 assert equal 都有不同的变化,但都应该什么都不返回,例如):
testSafeHeadForEmptyList :: Test
testSafeHeadForEmptyList =
TestCase $ assertEqual "Should return Nothing for empty list"
Nothing (safeHead ([]::[Int]))
testSafeHeadForEmptyList2 :: Test
testSafeHeadForEmptyList2 =
TestCase $ assertEqual "Should return Nothing for empty list"
Nothing (safeHead ([]::[Int]))
...
testSafeHeadForEmptyList99 :: Test
testSafeHeadForEmptyList99 =
TestCase $ assertEqual "Should return Nothing for empty list"
Nothing (safeHead ([]::[Int]))
Is this possible in Haskell?这在 Haskell 中是否可行?
If you look carefully here: Test
- you see that TestCase
and TestList
are data-constructors of the Test
type - so you should be able to do something like this:如果你仔细看这里:
Test
- 你看到TestCase
和TestList
是Test
类型的数据构造器 - 所以你应该能够做这样的事情:
testSafeHeadForEmptyList :: Test
testSafeHeadForEmptyList = TestList $
[ TestCase $ assertEqual "Should return Nothing for empty list"
Nothing (safeHead ([]::[Int]))
, TestCase $ assertEqual "Should return Nothing for empty list"
Nothing (safeHead ([]::[Int]))
...
, TestCase $ assertEqual "Should return Nothing for empty list"
Nothing (safeHead ([]::[Int]))
]
or even fmap
the TestCase
-constructor if you like:如果您愿意,甚至可以
fmap
TestCase
构造函数:
testSafeHeadForEmptyList :: Test
testSafeHeadForEmptyList = TestList $ fmap TestCase
[ assertEqual "Should return Nothing for empty list"
Nothing (safeHead ([]::[Int]))
, assertEqual "Should return Nothing for empty list"
Nothing (safeHead ([]::[Int]))
...
, assertEqual "Should return Nothing for empty list"
Nothing (safeHead ([]::[Int]))
]
and of course TestLabel
is a constructor as well so you can stick a Label to it:当然
TestLabel
也是一个构造函数,因此您可以将 Label 粘贴到它上:
testSafeHeadForEmptyList :: Test
testSafeHeadForEmptyList =
TestLabel "safe head for empty list" $ TestList $ fmap TestCase
[ assertEqual "Should return Nothing for empty list"
Nothing (safeHead ([]::[Int]))
, assertEqual "Should return Nothing for empty list"
Nothing (safeHead ([]::[Int]))
...
, assertEqual "Should return Nothing for empty list"
Nothing (safeHead ([]::[Int]))
]
(you could use (~:)
for that too of course) (当然,你也可以使用
(~:)
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.