本文介绍了在Google Test框架中测试用例中访问argc和argv的方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Google测试来测试我的C ++项目。

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

main()中,可以使用argc和argv来加载所需的数据。方法,初始化时,将argc和argv传递给测试的构造函数。

In the main() method, when initializing, argc and argv are passed to the constructor of testing.

testing::InitGoogleTest(&argc, argv);

如何在测试中稍后才能访问它们?

How can I access them later in a test?

TEST(SomeClass, myTest)
{
  // Here I would need to have access to argc and argv
}


推荐答案

我不知道google的测试框架,方法来做到这一点,但这应该做:

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
//---------------------------------------------

全局不是很漂亮,但是当你拥有一个测试框架,不允许你从 main()到你有什么测试功能,他们做的工作。

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.

这篇关于在Google Test框架中测试用例中访问argc和argv的方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-01 23:58