Is it possible to assert an error case in HUnit?

994 views Asked by At

If I have a function which results in an error for a certain input, is it possible to write a test verifying the error occurs for that input?

I do not find this "assert error" functionality available in HUnit. Is it available in HUnit or perhaps in some other test package?

1

There are 1 answers

0
Chad Gilbert On BEST ANSWER

You can catch an error and assert if it doesn't happen using standard exception handling:

errored <- catch (somethingThatErrors >> pure False) handler
if errored then
    assertFailure "Did not catch expected error"
else
    pure ()
where
   handler :: ErrorCall -> IO Bool
   handler _ = pure True