简体   繁体   中英

Google Test - Using "SetUpTestSuite" doesn't seem to work

I'm trying to write a test suite that performs a test-suite level "Set Up" operation.

I attempted to write a simple program first to try and get it working but I am not having any luck getting the "SetUpTestSuite" method to be called.

#include <gtest/gtest.h>
#include <iostream>

class MyTest : public ::testing::Test
{
protected:
    static void SetUpTestSuite() {
        std::cerr << "TestSuiteSetup" << std::endl;
    }

    static void TearDownTestSuite() {

    }
};

TEST_F(MyTest, Case1) {
    std::cerr << "TESTING" << std::endl;
}

int main(int argc, char* argv[])
{
    ::testing::InitGoogleTest(&argc, argv);
    return RUN_ALL_TESTS();
}

When I run this I get:

[==========] Running 1 tests from 1 test cases.
[----------] Global test environment set-up.

[----------] 1 test from MyTest
[ RUN      ] MyTest.Case1
TESTING
[       OK ] MyTest.Case1 (0 ms)
[----------] 1 test from MyTest (0 ms total)

[----------] Global test environment tear-down
[==========] 1 tests from 1 test cases ran. (0 ms total)
[  PASSED  ] 1 tests.

For some reason SetUpTestSuite() is never called.


I have been reading through the Sharing Resources Between Tests in the Same Suite section of the Google Test documentation but I can't figure out what I am doing wrong.

Is there something I am missing?


Note: I am using gtest v1.6.0 - it is the only package that was available from my companies Red Hat RPM repository.

The documentation appears to be wrong. These methods should be called SetUpTestCase() and TearDownTestCase() . At least in Google Test 1.8.0.

The change does not appear to have been released. The documents appear to be current with the Master branch, not the released version.

If your code-under-test throws exceptions, the gtest framework will catch them and still shutdown using TestDownTestSuite() . BUT if you launch some threads in your test case and they throw, then there is nothing to catch them and your process will likely call terminate() and the process will immediately halt and there will be no tear down. Be sure to catch exceptions in worker threads, or use std::async to create std::future s, which will automatically catch exceptions and rethrow them in the context of the original thread (when you call future.get() ) which will be caught by gtest.

This happened to me I started using the unsupported TEST_TIMEOUT_BEGIN() and TEST_TIMEOUT_FAIL_END() macro hacks ( http://antonlipov.blogspot.com/2015/08/how-to-timeout-tests-in-gtest.html ) and then exceptions started happening.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM