BAdhi
BAdhi

Reputation: 510

Using gtest specific parameters inside the testcase

Im using gtest as the test framework for my project and I need to use the following parameter value which is passed as a gtest parameter, inside the test case

./gtest_bin --gtest_repeat=5

Currently what I'm doing is, I've created a custom Listener extended from "EmptyTestEventListener" and pass the iteration value to a global variable when the callback to the following function is received as following,

customListener class

class customListener : public testing::EmptyTestEventListener
{
    customListener(int *iteration) : m_iteration(iteration) {}

    virtual void OnTestIterationStart(const testing::UnitTest& unit_test, int iteration)
    {  
        *m_iteration = iteration;
    }
}

main

int g_iteration;

int main(int argc, char **argv) 
{
    testing::InitGoogleTest(&argc, argv);

    if(argc > 1)
        g_array_length = atoi(argv[1]);

    testing::TestEventListeners& listners = testing::UnitTest::GetInstance()->listeners();
    listners.Append(new printers::customListener(&g_iteration));

    return RUN_ALL_TESTS();
}

Is there an easier way to get this gtest parameter value?

If generalized, Is there a way to get any gtest specific parameter value that can used inside a testcase?

Upvotes: 1

Views: 1133

Answers (1)

PiotrNycz
PiotrNycz

Reputation: 24430

IMPORTANT NOTE Please use with great care - as this is not documented feature and might be changed (i.e. it might stop working) in next gtest/gmock versions.

You can access any flag by ::testing::GTEST_FLAG(flag_name) - in your case - ::testing::GTEST_FLAG(repeat).

I got this information indirectly from this manual and by examining gtest.h file.

Upvotes: 1

Related Questions