How can I expect multiple failures in google test?

c++, googletest

Solution

I had the same problem and the one approach that works is:

EXPECT_NONFATAL_FAILURE({
    EXPECT_NONFATAL_FAILURE(failTwice(), "");
},"Actual: 2");

With the "Actual: 2" I set the expectation that 2 non-fatal failures will occur. One drawback is that it is not easy to tell which error messages you are expecting.

Problem

How can I expect multiple failures in google test? I use this when testing that asserts happen in my code under test. Because these asserts are not fatal, multiple can happen. The following testcase reproduces this: ``` void failTwice() { EXPECT_TRUE(false) << "fail first time"; EXPECT_TRUE(false) << "fail second time"; } TEST_F(FailureTest, testMultipleFails) { EXPECT_NONFATAL_FAILURE(failTwice(), "time"); } ``` This produces the following output: ``` gtest/src/gtest.cc:657: Failure Expected: 1 non-fatal failure Actual: 2 failures FailureTest.h:20: Non-fatal failure: Value of: false Actual: false Expected: true fail first time FailureTest.h:20: Non-fatal failure: Value of: false Actual: false Expected: true fail second time ``` The problem is this: Expected: 1 non-fatal failure How can I tell google test to expect multiple failures?

Original source