Is it possible to assert an error case in HUnit?

Viewed 703

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 Answers

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
Related