How to test an EXE with Google Test?

后端 未结 6 1410
梦毁少年i
梦毁少年i 2020-12-03 05:55

I have a C++ project in Visual Studio, and have added another project exclusively for testing. Both of these projects are EXEs (console apps). So how do I use the first proj

6条回答
  •  旧巷少年郎
    2020-12-03 06:16

    Depends. Google Test is (primarily) a Unit Testing framework (oversimplifying, testing classes). You can absolutely use is for other types of tests, but it doesn't have "built in" functionality for other types of testing, you'll have to write it yourself.

    If you are trying to system test your executable, than you can run the process. I suggest using Boost.Process if you are using a multi-platform system or already have a boost dependency. Else, look here: launch an exe/process with stdin stdout and stderr?

    The "tests" you write will call the executable, and can input stdin or stdout accordingly.

    For example:

    std::string path_to_exectuable = "thepath";
    TEST(FooTester,CheckHelpScriptReturns0)
    {
     using bp =::boost::process; 
     std::vector args; args.push_back("--help");
     bp::context ctx; 
     ctx.stdout_behavior = bp::capture_stream(); 
    
     bp::child c = bp::launch(exec, args, ctx); 
     bp::status s = c.wait(); 
     ASSERT_TRUE(s.exited())<<"process didn't exit!";
     ASSERT_EQ(s.exit_status(),0)<<"Help didn't return 0";
    }
    

提交回复
热议问题