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?
JUnit Test Exception Examples - How to assert an exception is thrown 1 Test Exception in JUnit 5 - using assertThrows () method#N#JUnit 5 provides the assertThrows () method that asserts a... 2 Test Exception in JUnit 4#N#In JUnit 4.7 or above, you can test exception by using the @Rule annotation with an... 3 Test Exception in JUnit 3 More ...
HUnit is a unit testing framework for Haskell, inspired by the JUnit tool for Java. This guide describes how to use HUnit, assuming you are familiar with Haskell, though not necessarily with JUnit. A test-centered methodology for software development is most effective when tests are easy to create, change, and execute.
Unit testing frameworks for various languages are referred to as xUnit . With HUnit, as with xUnit, you can easily create tests, name them, group them into suites, and execute them, with the framework checking the results automatically. HUnit currently includes only a text-based test controller, but the framework is designed for easy extension.
If the expected exception ( IllegalArgumentException in this example) is thrown, the test succeeded, otherwise it fails. You can see the above code uses an anonymous class of type Executable. Of course you can shorter the code with Lambda syntax:
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With