Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GoogleTest: Accessing the Environment from a Test

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.

like image 359
jco Avatar asked Mar 12 '10 19:03

jco


People also ask

What is a test environment?

A test environment is a server that allows you to run the test cases you have defined. The test environment includes more than just setting up a server to run tests on. It also involves hardware and network configuration. In other words, a test environment enables you to create identical environments every time you need to test your product.

Why are test environments not identical?

This is exactly why test environments are not identical. They often need a different setup for testing different parts of the code. In addition, it’s important to understand that the testing engineer creates dozens of test environments as the code evolves. In short, when using a test environment, the code dictates the environment’s setup.

Is the getstarttime() method available in the testenvironment object?

This is not the answer. 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.

What are the functions of googletest?

GoogleTest defines the following functions to help with writing and running tests. Initializes GoogleTest. This must be called before calling RUN_ALL_TESTS (). In particular, it parses the command line for the flags that GoogleTest recognizes.


2 Answers

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();
}
like image 78
thclark Avatar answered Oct 13 '22 01:10

thclark


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);
like image 41
Marvin Avatar answered Oct 13 '22 01:10

Marvin