I am writing some unit tests in a C++14 project where I often want to check a large number of invariants. The natural way to do this is to create a method:
void validateState(const Model& model, int offset) {
ASSERT_EQ(model.ax * model.bx, model.cx * offset);
ASSERT_NE(model.base, offset);
...
}
The problem is if these asserts fail, I get the line number of the method. Is there a way to get googletest to print the line number of the caller, and the specific assert that failed?
Yes, there is a built-in way: SCOPED_TRACE (see https://github.com/google/googletest/blob/master/googletest/docs/advanced.md#using-assertions-in-sub-routines) does exactly what you want.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With