Search code examples
c++unit-testinggoogletest

Compound Tests with GoogleTest?


If I have something like:

ASSERT_TRUE(RANGE(val1, val2, abs_err) || RANGE(val1, val3, abs_err));

How would I use ASSERT_NEAR instead of ASSERT_TRUE?

I've attempted to break up the statement into two ASSERT_NEAR statements, like below, but the test fails.

ASSERT_NEAR(val1, val2, abs_err);
ASSERT_NEAR(val1, val3, abs_err);

Solution

  • Your use case is: any of these two condition should be true. So use ::testing::AnyOf(m1,m2,...)!

    Equivalent of ASSERT_NEAR(lhs,rhs,max_error) in matchers world is ::testing::DoubleNear(rhs,max_error) - so your example will look like this:

    ASSERT_THAT(val1, AnyOf(DoubleNear(val2, abs_err),
                            DoubleNear(val3, abs_err)));
    

    If you need both of your conditions are true - use ::testing::AllOf - actually your attempt is just equivalent of AllOf - that is why it failed.