Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way for googletest to generate caller line numbers for asserts in a called function?

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?

like image 627
Chuu Avatar asked Dec 15 '25 14:12

Chuu


1 Answers

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.

like image 100
Lorenz Zhao Avatar answered Dec 17 '25 03:12

Lorenz Zhao



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!