I am using gtest to create unit tests to my c++ program. In my tests I have to write a lot of checks like this:
ASSERT_TRUE(myObject.IsValid());
EXPECT_EQ(myObject.GetSomeAttribute(), expectedValue);
I have to write both checks because if I omit the ASSERT_TRUE
and myObject
happened to be not valid, than myObject.GetSomeAttributre()
call crashes. That's not good even in tests.
What I want is to write something like:
EXPECT_XXX_EQ(myObject.GetSomeAttribute(), expectedValue);
This line of code should do approximately the same as the original two lines (with optional bonus that if myObject
is not valid, this will be reported, GetSomeAttribute()
would not be called, but the test will continue running).
How can I write such custom assert/expect?