Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EXPECT_NO_DEATH() in Google Test

There are the useful EXPECT_DEATH() and family of rules to check your program dies as expected, but is there a negative EXPECT_NO_DEATH() set or similar? As an artificial example:

void should_i_die(bool die)
{
    if (die) { printf("Aaargh!"); exit(-1); }
    else       printf("I'm not dead yet!");
}

EXPECT_DEATH(should_i_die(true), "Aaargh.*");
EXPECT_NO_DEATH(should_i_die(false), ".*"); // What should be here?

Having just a stand-alone:

should_i_die(false);
EXPECT_TRUE(true); // We're not dead if we reach here

Feels like a bit of a cop-out, so is there a better way?

like image 669
Ken Y-N Avatar asked Sep 17 '25 01:09

Ken Y-N


1 Answers

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.

like image 106
Hasturkun Avatar answered Sep 18 '25 18:09

Hasturkun