6

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?

mherzl
  • 5,624
  • 6
  • 34
  • 75
  • 2
    The `HUnit-Plus` package provides [`assertThrows`](https://hackage.haskell.org/package/HUnit-Plus-2.0.0/docs/Test-HUnitPlus-Base.html#v:assertThrows). – Alexis King Sep 20 '17 at 21:12

1 Answers1

5

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
Chad Gilbert
  • 36,115
  • 4
  • 89
  • 97