user6245072 user6245072 - 2 months ago 12
C++ Question

Is the rest of the code in a test executed even if an EXPECT fails?

I am trying to get myself into Test Driven Developement, and I have a question on the framework Google Test:

If I have a test like

TEST(SampleTest1, check_and_delete) {
MyClass* obj = func(data);
EXPECT_EQ(data2, obj->attr);
delete obj;
}


Will the last line, where I free used memory, execute even if the above EXPECT failed?

If not, what macros should I use to get the result I want (i.e. test if two things are equal, but still execute memory cleaup)?

Answer

I cant remember with certainty if google test continues to process a test after the first failure, but i think it does. To test simply add in a bogus test and see if it executes

TEST(SampleTest1, check_and_delete) {
    MyClass* obj = func(data);
    EXPECT_EQ(data2, obj->attr);
    int i = 5, j=5;
    EXPECT_EQ(i, j);
    delete obj;
}

if the second test is evaluated then you know that the test will continue after a failure. If it doesn't evaluate the second expect_eq then you will need to make a local copy on the stack then delete the pointer before the expect_eq

TEST(SampleTest1, check_and_delete) {
    MyClass* obj = func(data);
    auto objAttr= obj->attr;
    delete obj;
    EXPECT_EQ(data2, objAttr);
}
Comments