Reputation: 319
I am using Boost.Assert library, and have this custom assert code, which needs to be unit tested using Google Test framework:
#include <boost/assert.hpp>
#define ASSERT(expr) BOOST_ASSERT(expr)
#define ASSERT_MSG(expr, msg) BOOST_ASSERT_MSG(expr, msg)
namespace boost {
inline void assertion_failed_msg(char const* expr, char const* msg, char const* function, char const* /*file*/, long /*line*/) {
std::cerr << "Expression '" << expr << "' is false in function '" << function << "': " << (msg ? msg : "<...>") << ".\n"
<< "Backtrace:\n" << boost::stacktrace::stacktrace() << std::endl;
std::abort();
}
inline void assertion_failed(char const* expr, char const* function, char const* file, long line) {
::boost::assertion_failed_msg(expr, 0 /*nullptr*/, function, file, line);
}
} // namespace boost
The idea is to check that ASSERT( 1!=1 )
terminates the program with the appropriate error code and error log message.
I am aware of Google Test, Death Tests. I have the following structure, which kind of works for me:
void assert_death_test()
{
ASSERT( 1!=1 );
}
TEST(unit_test_DeathTest, test_of_assert_function) {
EXPECT_EXIT(assert_death_test(), ::testing::KilledBySignal(SIGABRT), "Stuff hit the fan.");
}
Since I'm using a death test, ASSERT(...)
does not terminate unit testing, and death test notices that the program exits with some log message. The problem is this:
Death test: assert_death_test()
Result: died but not with expected error.
Expected: contains regular expression "Stuff hit the fan."
Actual msg:
[ DEATH ] Expression '1!=1' is false in function 'void assert_death_test()': <...>.
[ DEATH ] Backtrace:
[ DEATH ] 0# boost::assertion_failed_msg(char const*, char const*, char const*, char const*, long) in ./executable
[ DEATH ] 1# boost::assertion_failed(char const*, char const*, char const*, long) in ./executable
[ DEATH ] 2# assert_death_test() in ./executable
[ DEATH ] 3# unit_test_DeathTest_test_of_assert_function_Test::TestBody() in ./executable
.
. // Continues with more log messages
.
Because of this problem, the test is counted as a fail, whereas it normally is a success. (The assert kills program, and sends a log message to stdout)
How can I solve this problem? (Alternative methods are also applicable)
Is there a way to force the output of a test to be successful?
Upvotes: 1
Views: 935
Reputation: 319
The problem is solved with the following line:
EXPECT_DEATH(assert_death_test(), "Expression '.*");
My main issue was not creating a vaild regex string, as pointed out by @Quarra.
EXPECT_DEATH
is also more appropriate, as ASSERT(...)
kills the program.
Upvotes: 0
Reputation: 2695
You're setting an expectation that "Stuff hit the fan."
will be logged at program exit but your assertion doesn't have any message so this might be the problem here. The test fails because you're expecting a custom message to be logged which is not added there. Please try:
void assert_death_test()
{
ASSERT( 1!=1, "Stuff hit the fan.");
}
As you're implementing a custom message here, I'd also add this to the expected output, i.e. the expected string should also contain "Expression '"
...
Upvotes: 1