Separate test cases across multiple files in google test

后端 未结 4 1286
自闭症患者
自闭症患者 2020-12-17 07:53

I\'m new in google test C++ framework. It\'s quite easy to use but I\'m wondering how to separate the cases into multiple test files. What is the best way?

Include t

4条回答
  •  醉酒成梦
    2020-12-17 08:47

    Create one file that contains just the main to run the tests.

    // AllTests.cpp
    #include "gtest/gtest.h"
    
    int main(int argc, char **argv)
    {
        testing::InitGoogleTest(&argc, argv);
        return RUN_ALL_TESTS();
    }
    

    Then put the tests into other files. You can put as many tests as you like in a file. Creating one file per class or per source file can work well.

    // SubtractTest.cpp
    #include "subtract.h"
    #include "gtest/gtest.h"
    
    TEST(SubtractTest, SubtractTwoNumbers)
    {
        EXPECT_EQ(5, subtract(6, 1));
    }
    

    This does require that all tests can share the same main. If you have to do something special there, you will have to have multiple build targets.

提交回复
热议问题