Ken Y-N
Ken Y-N

Reputation: 15018

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?

Upvotes: 4

Views: 2470

Answers (1)

Hasturkun
Hasturkun

Reputation: 36412

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.

Upvotes: 4

Related Questions