googletest: Fatal failure in SetUpTestCase() function should skip running tests.

I think googletest should skip running tests (or automatically mark as
failed) in a particular fixture if we call SetUpTestCase() function
and there is fatal failure in it. Suppose we create some shared object
in SetUpTestCase() function and try to access that in TEST_F. But the
failure to create that object in SetUpTestCase() cause to access NULL
inside TEST_F.

So if we make fatal assertion in SetUpTestCase() itself, then above
problem can be avoided.


Original issue reported on code.google.com by pvshew...@gmail.com on 11 Jan 2010 at 6:39

About this issue

  • Original URL
  • State: closed
  • Created 9 years ago
  • Reactions: 6
  • Comments: 18 (9 by maintainers)

Commits related to this issue

Most upvoted comments

Here as well. Please take a look!

Hello, any progress in this issue? I have the same problem.

Is there any progress on this issue? These seems like a major issue to me and should be prioritized.

wow , 10 years.

It’s been hanging here since 2010…

I face this issue with gtest1.8, and when it will fix? I don’t think test event listener is a good way to fix it by myself.

Guys! Any progress o this topic?? It’s been hanging here since 2015…

You can “fix” this yourself by using a test event listener, as described in the advanced user guide, by doing something like this in your derived test event listener class (which you would register in your main()):

class TestEventListener : public ::testing::EmptyTestEventListener
{
    // Fired before the test case starts and before SetUpTestCase() is invoked.
    virtual void OnTestCaseStart(const ::testing::TestCase& test_case) override
    {
        firstTestInTestCase = true;
        testCaseHadFailure = false;
    }

    // Fired before each individual test starts: before the test fixture is constructed
    // and SetUp() is invoked.
    virtual void OnTestStart(const ::testing::TestInfo& info) override
    {
        if (firstTestInTestCase) {
            size_t numFailures = numFatalFailures();

            if (numFailures > previousNumFatalFailures) {
                testCaseHadFailure = true;
                previousNumFatalFailures = numFailures;
            }

            firstTestInTestCase = false;
        }

        if (testCaseHadFailure) {
            // this will avoid running the test fixture's SetUp() and
            // TearDown() and TEST()/TEST_F(), but the test fixtures
            // constructor is still invoked (that's unavoidable)
            FAIL() << "The test case's SetUpTestCase() or executable had a fatal failure";
        }
    }

    // gets the number of fatal failures in the UnitTest's ad_hoc_test_result,
    // which is the number of fatal failures other than those inside of test
    // cases; fatal failures in SetUpTestCase() also count in this, but those
    // in SetUp(), TearDown(), and TEST()/TEST_F() do not.
    size_t numFatalFailures()
    {
        const auto& result = ::testing::UnitTest::GetInstance()->ad_hoc_test_result();
        const int numParts = result.total_part_count();
        size_t failures = 0;

        for (int i=0; i < numParts; ++i) {
            if (result.GetTestPartResult(i).fatally_failed()) {
                ++failures;
            }
        }

        return failures;
    }

    bool   firstTestInTestCase{true};
    bool   testCaseHadFailure{false};
    size_t previousNumFatalFailures{0};
};

And then register the event listener in your main(), with something like this:

    // Gets hold of the event listener list
    ::testing::TestEventListeners& listeners = ::testing::UnitTest::GetInstance()->listeners();
    // Adds a listener to the end.  Google Test takes the ownership.
    listeners.Append(new test::TestEventListener);