Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling one TEST_F in another TEST_F in gtest

I have a test suite in gtest.My Test Fixture class has two tests,one is Test_F(functional),Test_F(performance). I have the implementation in such a way that Test_F(performance) is dependent on the run of Test_F(functional). I want to handle the case,where when Test_F(functional) is disabled.So,my approach is calling Test_F(functional) from inside Test_F(performance). I am not sure,how this is done in gtest,calling a test_function,inside another test_function. Any help is appreciated.

like image 204
Priyanka M N Avatar asked Oct 25 '25 06:10

Priyanka M N


1 Answers

AFAIK there's no option to call test function inside another test function. However, to share the code between test cases, you can define a helper method in the test suite:

class FooBarTestSuite : public ::testing::Test {
public:
    TestSuite() {
        // init code goes here
        // foo and bar are accessible here 
    }

    ~TestSuite() {
        // deinit code goes here
        // foo and bar are accessible here 
    }

    void CommonCode() {
        // common test case code here
        // foo and bar are accessible here 
    }

    ClassFoo foo{};
    ClassBar bar{};
};

TEST_F(FooBarTestSuite, PerformanceTest) {
    CommonCode();
    // performance-specific code here
}

TEST_F(FooBarTestSuite, FunctionalTest) {
    CommonCode();
    // functional-specific code here
}

In order to run a specific test case, use --gtest_filter=*Performace* or --gtest_filter=*Functional* as the parameter.

like image 134
Quarra Avatar answered Oct 27 '25 20:10

Quarra



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!