Search code examples
catch-unit-test

Point of REQUIRE_NOTHROW in the catch c++ testing framework


What is the point of the REQUIRE_NOTHROW assertion? If I just put a statement and don't wrap it in any assertion macro it will fail if it throws anyway?


Solution

  • It's the difference between the TEST_CASE failing and an individual assertion failing. The REQUIRE macros ensure that the next lines aren't executed if they fail. Conversely, the CHECK macros can mark the test case as a failure but continue.

    Consider this example:

    REQUIRE_NOTHROW(parseInput(validInput));
    REQUIRE_THROWS(parseInput(errorInput));
    REQUIRE_THROWS(parseInput(NULL));
    

    So we're explicitly requesting that passing valid input does not cause an exception, but bad input does. If we didn't use the REQUIRE_NOTHROW() macro, then the test would fail but then we'd need to decipher where it failed - an exception could have come from other lines of test code.