snitch: Support automatic check for unexpected exception
In my tests with Catch2, I would expect everything in test case to be checked for exceptions. If anything outside of REQUIRE_THROWS / CHECK_THROWS throws an exception, I consider it a failed test, and I want to get a message about which section / line caused the exception.
It seems Snitch doesn’t support this scenario currently. For example
#define SNITCH_IMPLEMENTATION
#include "snitch_all.hpp"
#include <exception>
void command1() {}
void command2() {}
void command3() {throw std::runtime_error("error");}
TEST_CASE("commands") {
SECTION("command 1") {
command1();
}
SECTION("command 2") {
command2();
}
SECTION("command 3") {
command3();
}
}
outputs:
starting a with snitch v1.1.1.e557246
==========================================
failed: running test case "commands"
at <snitch internal>:0
unhandled std::exception caught; message: whoops
==========================================
error: some tests failed (1 out of 1 test cases, 0 assertions, 9.610000e-05 seconds)
Only if I wrap command3() in REQUIRE_NOTHROW, will I get a nice error message. But wrapping every command in every test in macro is unfeasible.
Am I missing something, or is this a necessary tradeoff for smaller footprint?
About this issue
- Original URL
- State: closed
- Created 7 months ago
- Comments: 18 (13 by maintainers)
Commits related to this issue
- Add checks for exceptions in all test macros for #141 — committed to snitch-org/snitch by cschreib 7 months ago
- Add checks for exceptions in all test macros for #141 — committed to snitch-org/snitch by cschreib 7 months ago
- Don't unwind section data on uncaught exception for #141 — committed to snitch-org/snitch by cschreib 7 months ago
- Keep track of current location to report uncaught exception for #141 — committed to snitch-org/snitch by cschreib 7 months ago
- Keep track of current location to report uncaught exception for #141 — committed to snitch-org/snitch by cschreib 7 months ago
I pushed an attempt at this. In summary:
CHECK(...), the message will say “somewhere in check at file:line”CHECK(...)but inside aSECTION(...), the message will say “somewhere in section at file:line”How does that sound?
After a bit of refactoring, I managed to get this to work with almost zero net overhead (it did add a bit of overhead, but the refactoring was enough to gain back most of it by simplifying the assertion reporting functions).
So I think this can be enabled all the time. PR to follow: https://github.com/snitch-org/snitch/pull/151