What's the way to access argc and argv inside of a test case in Google Test framework?

后端 未结 3 504
予麋鹿
予麋鹿 2021-01-11 14:42

I’m using Google Test to test my C++ project. Some cases, however, require access to argc and argv to load the required data.

In the main() method, when

3条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-11 15:21

    I don't know google's test framework, so there might be a better way to do this, but this should do:

    //---------------------------------------------
    // some_header.h
    extern int my_argc;
    extern char** my_argv;
    // eof
    //---------------------------------------------
    
    //---------------------------------------------
    // main.cpp
    int my_argc;
    char** my_argv;
    
    int main(int argc, char** argv)
    {    
      ::testing::InitGoogleTest(&argc, argv);
      my_argc = argc;
      my_argv = argv;
      return RUN_ALL_TESTS();
    }
    // eof
    //---------------------------------------------
    
    //---------------------------------------------
    // test.cpp
    #include "some_header.h"
    
    TEST(SomeClass, myTest)
    {
      // Here you can access my_argc and my_argv
    }
    // eof
    //---------------------------------------------
    

    Globals aren't pretty, but when all you have is a test framework that won't allow you to tunnel some data from main() to whatever test functions you have, they do the job.

提交回复
热议问题