Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to assert an error case in HUnit?

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?

like image 524
mherzl Avatar asked Sep 20 '17 19:09

mherzl


People also ask

How to assert an exception is thrown in JUnit?

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 ...

What is a hunit test?

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.

What is the difference between hunit and xUnit?

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.

What happens if the expected exception is thrown in a test?

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:


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
like image 125
Chad Gilbert Avatar answered Nov 24 '22 09:11

Chad Gilbert