简体   繁体   中英

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

To quote:
Let's assume that we have module SafePrelude.hs :

module SafePrelude where

safeHead :: [a] -> Maybe a
safeHead []    = Nothing
safeHead (x:_) = Just x

we can put tests into TestSafePrelude.hs as follows:

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 ? 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):

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?

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:

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:

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:

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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