Home > database >  catch a gtest EXPECT statement failure
catch a gtest EXPECT statement failure

Time:11-30

I have a test helper function containing various gtest expect e.g. EXPECT_TRUE(). I have a couple tests that I intentionally want to fail. Rather than complicating the test helper function parameters, is there a simple way to ignore the asserts for these specific tests? For example using a try/catch in the case of an exception.

#include <gtest/gtest.h>

class Foo: public testing::Test
{
  // fixture things...
};

void test_helper(bool val_a)
{
  EXPECT_TRUE(val_a);
  // more EXPECT validation goes here
}

TEST_F(Foo, failing_test)
{
    // can this next line be wrapped to expect a failure?
    test_helper(this, false);
}

CodePudding user response:

near the top of the testing/testing.h file there is the AssertionResult class with the description

"A class for indicating whether an assertion was successful..."

The only part of the original helper function signature that needs changing is the return statement.

testing::AssertionResult test_helper(bool val_a)
{
  if (!val_a)
  {
    return testing::AssertionFailure() << "custom error message...";
  }
  // more validation goes here
  return testing::AssertionSuccess();
}

TEST_F(Foo, failing_test)
{
    // This test now passes
    ASSERT_FALSE(test_helper(this, false));
}

CodePudding user response:

When test fails you can do following this:

Sadly there is not functionality to mark some test as failing (as known bug), so it is run, but whole test suite is consider as success. Why? Reasoning is simple, once someone marks test to be fixed later it will not be fixed in reasonable time. Test should always pass otherwise their value is quite low.

  • Related