What are Google Test, Death Tests
The assertion is there to confirm that a function would bring about program termination if it were executed in the current process (the details explains that the death test is invoked from a subprocess which allows the tests to continue despite the death). This is useful because some code may guarantee program termination / abortion on failure (e.g. if there was an irrecoverable error), and unit tests should confirm that a function adheres to its documented behavior, regardless of what that might be.
The description on the wiki page really explains it best:
In many applications, there are assertions that can cause application failure if a condition is not met. These sanity checks, which ensure that the program is in a known good state, are there to fail at the earliest possible time after some program state is corrupted. If the assertion checks the wrong condition, then the program may proceed in an erroneous state, which could lead to memory corruption, security holes, or worse. Hence it is vitally important to test that such assertion statements work as expected.
Google death tests without message
Turned out there have been two misunderstandings on my side:
- The matcher-strings are sub-string patterns. This means an empty matcher-string matches anything (not only empty messages as assumed by me).
Actual msg
did not really display an empty message string. It only uses a weird formatting. Google test adds a line break followed by[ DEATH ]
afterActual msg
before printing the error message. This made me think I would see the un-caught error output on the console but actually it was the correctly detectedActual msg
.
Knowing those facts, everything works as expected.
Why does the gtest death test loop indefinitely
For anyone having the same problem:
The main problem lies in testing::InitGoogleTest()
.
When gtest finds a death test, it creates a new process using createProcessA(...)
under Windows and passes additional parameters, to make sure the child process only executes the current death test.
This leads to problems, when those parameters are not given to testing::InitGoogleTest()
in the child process. In this case it doesn't receive the information to only execute the death test and starts running the whole test code again, leading to recursion.
TLDR:
Pass the command line arguments from main(int argc, char *argv[])
to testing::InitGoogleTest()
:
testing::InitGoogleTest(argc, argv)
Using mocks and death tests
This is a known limitation of death tests. Internally assert_death
forks, so mocks called in the child are not registered in the parent process. If you wish to suppress the warnings, consider using NiceMock
.
Google test error: '*' can only follow a repeatable token
The character *
is reserved by the regular expression grammar to indicate matching zero or more of the previous tokens or groups.
Some simple examples:
.*
matches zero or more of any charactera*
matches zero or more of the charactera
[A-F]*
matches zero or more of the charactersA
through toF
The error is occurring because you have a *
at the beginning of the string, where there is no preceding character, group etc to be repeated. This is essentially a syntax error in the regular expression grammar, and the error message tells you so.
What you actually want is a literal *
, not the one that belongs to the grammar. To achieve this, you must escape it with a \
. And because strings in C++ also use that for an escape character, you must escape the backslash too. So you need two backslashes before the *
(or use a raw string literal).
In a pinch, the solution should be:
EXPECT_EXIT(test_Death(), testing::ExitedWithCode(EXIT_FAILURE), "\\*Error\n");
EXPECT_NO_DEATH() in Google Test
I think something like this should work (though I haven't tried it myself).
EXPECT_EXIT({should_i_die(false); fprintf(stderr, "Still alive!"); exit(0);},
::testing::ExitedWithCode(0), "Still alive!");
The exit is needed AFAICT, as living past the end of the death test is considered a failure.
You could create a wrapper to make this less ugly.
Cannot capture the debug assertion in google test(death test doesn't satisfy it)
Googletest's death tests cannot suppress dialogs; the test program would spawn a separate process for running the test case and analyze how it exists and what it writes to the stderr. Instead of _ASSERTE
you can use standard C++ assert
; on failure it does what googletest wants it to do.
Related Topics
Why Default Return Value of Main Is 0 and Not Exit_Success
How to Create a N Way Cartesian Product of Type Lists in C++
When Did "And" Become an Operator in C++
What Is a Cross-Platform Way to Get the Current Directory
Why Can't Operator () of Stateless Functor Be Static
How to Read System Information in C++
How to Control Memory Allocation Strategy in Third Party Library Code
Read and Write on Serial Port in Ubuntu with C/C++ and Libserial
How to Define Thread-Local Local Static Variables
Neatest Way to Loop Over a Range of Integers
Template Metaprogramming: (Trait For) Dissecting a Specified Template into Types T<T2,T3 N,T4, ...>
Why Does Std::Map Not Have a Const Accessor
Register an Object Creator in Object Factory
How to Append to a File with Fstream Fstream::App Flag Seems Not to Work