How do I make gtest not completely shutdown when it hits assert? (do not validate tests)

There are many places in the developer's code where he calls assert (xyz):

(from assert.h)

#define assert(_Expression) (void)( (!!(_Expression)) || (_wassert(_CRT_WIDE(#_Expression), _CRT_WIDE(__FILE__), __LINE__), 0) )

When I run my tests with gtest and one of these assertions fails, my executable shuts down completely.

I want gtest to be able to just catch this statement, skip the test, and continue executing. Is it possible?

+3


source to share


2 answers


As from google testing reference documentation

How to write a death test

Google Test has the following macros to support death tests: enter image description herewhere statement is the statement that is expected to cause the process to die, predicate is a function or function object that evaluates to an integer exit status, and regex is the regex that is expected , will match the stderr output of the statement. Note that the operator can be any valid operator (including compound operator) and does not have to be an expression.

You can use these test macros to intercept native ones exit()

or _exit()

calls to your code under test if they return different values ​​from 0

.




As for your comment

"What if the test itself is not expecting this, but it still happens? I don't want the rest of my execution to stop. It just doesn't pass, then continue."

Sorry, you cannot prevent this. This is what the assert()

statements are for and acts as a self-assertion for certain functions that test the inputs or the conditions they achieve.
You can try to compile your testing and test code using a -DNDEBUG

compiler option , but that will lead to even more obscure issues affecting undefined behavior or the like.

If a test case can lead to an unexpected assertion, there is either something wrong with your test case input values ​​or the checked code.
So you have to set reproducible conditions so that either the test script fails with the assertion (and the slave block tester is executed), or the whole thing explodes (exits the test run process), which means your tested input fails (and you need will modify the test file or fix the code under test.)

+1


source


Basically, if the code you are testing doesn't work, the test cannot continue.

To minimize error, make sure that the code you are testing at least compiles correctly and the input collection is valid.

I say that this does not mean, but rather from personal experience. I am using gtest and gmock for my own projects. Lately I've been playing with code that was a little out of my league (after all, the only way to grow is to stretch beyond your intended limits).



The code was taking data from a data file, and this was causing my tests to fail, not because there was something wrong in that test, but because I was not doing correct error checking for functions that were read from the file, and this threw a key into things when the program received a string, and wanted an integer.

Believe it or not, exceptions are a good thing in tests. You don't want to just ignore them and move on, you want to figure out what's causing them and make it stop. This is the reason for testing.

0


source







All Articles