15

I'm trying out gtest for C++ (Google's unit testing framework), and I've created a ::testing::Environment subclass to initialize and keep track of some things that I need for most of my tests (and don't want to setup more than once).

My question is: How do I actually access the contents of the Environment object? I guess I could theoretically save the Environment in a global variable in my test project, but is there a better way?

I'm trying to make tests for some already existing (very tangled) stuff, so the setup is pretty heavy.

jco
  • 1,335
  • 3
  • 17
  • 29

2 Answers2

10

A related question deals with this for the specific case of creating a std::string, giving a full response showing how to use google's ::testing::Environment and then access the results from inside a unit test.

Reproduced from there (if you upvote me, please upvote them too):

class TestEnvironment : public ::testing::Environment {
public:
    // Assume there's only going to be a single instance of this class, so we can just
    // hold the timestamp as a const static local variable and expose it through a
    // static member function
    static std::string getStartTime() {
        static const std::string timestamp = currentDateTime();
        return timestamp;
    }

    // Initialise the timestamp in the environment setup.
    virtual void SetUp() { getStartTime(); }
};

class CnFirstTest : public ::testing::Test {
protected:
    virtual void SetUp() { m_string = currentDateTime(); }
    std::string m_string;
};

TEST_F(CnFirstTest, Test1) {
    std::cout << TestEnvironment::getStartTime() << std::endl;
    std::cout << m_string << std::endl;
}

TEST_F(CnFirstTest, Test2) {
    std::cout << TestEnvironment::getStartTime() << std::endl;
    std::cout << m_string << std::endl;
}

int main(int argc, char* argv[]) {
    ::testing::InitGoogleTest(&argc, argv);
    // gtest takes ownership of the TestEnvironment ptr - we don't delete it.
    ::testing::AddGlobalTestEnvironment(new TestEnvironment);
    return RUN_ALL_TESTS();
}
thclark
  • 4,784
  • 3
  • 39
  • 65
  • 1
    This answer does not provide an information "how to access to environment object". It shows static function call. This is not the answer. – shs_sf Feb 05 '22 at 10:19
  • The answer is a full demonstration of how to set it up, including how to access the TestEnvironment object within the test fixtures. The TestEnvironment shown IS the environment object, getStartTime() is one of its attributes. This answer is 3 years old now, and I've switched to Rust so not keeping up with googletest. Maybe there are less cumbersome ways to do it now; if so you should do that and provide your own answer, @shs_sf. – thclark Feb 05 '22 at 11:51
4

Using a global variable seems to be the recommended way, according to the Google Test Documentation:

::testing::Environment* const foo_env = ::testing::AddGlobalTestEnvironment(new FooEnvironment);
Drew Dormann
  • 59,987
  • 13
  • 123
  • 180
Marvin
  • 482
  • 5
  • 19
  • 5
    Actually they seem to strongly recommend to not do that: _"However, we strongly recommend you to write your own main() and call AddGlobalTestEnvironment() there ..."_. – Zitrax Jan 23 '20 at 21:27