C++ 如何在 GoogleTest 中运行特定的测试用例
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12076072/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to run specific test cases in GoogleTest
提问by Rasmi Ranjan Nayak
I am trying to write a function/method for my project, which will ask to user which all test cases are you going to run? It looks like below...,
我正在尝试为我的项目编写一个函数/方法,它会询问用户您要运行哪些所有测试用例?看起来像下面...,
Test_Cases_1
|_TestNo1
|_TestNo2....so on
Test_Cases_2
|_TestNo1
|_TestNo2....so on
....
....so on
Test_Cases_N
|_TestNo1
|_TestNo2....so on
So, now the challenge is while running the project it should prompt me what all test cases you would like to execute?
If I select Test_Cases_1
and Test_Cases_N
. Then it should execute these two test cases and should exclude all other from Test_Cases_2 to ....
. In result window also I would like to see the results of Test_Cases_1
and Test_Cases_N
.
所以,现在的挑战是在运行项目时,它应该提示我你想要执行的所有测试用例是什么?如果我选择Test_Cases_1
和Test_Cases_N
。然后它应该执行这两个测试用例并且应该从Test_Cases_2 to ....
. 在结果窗口也是我希望看到的结果Test_Cases_1
和Test_Cases_N
。
So, if I will see the GoogleTest, there is a method called test_case_to_run_count()
;
But all the test cases
are getting registered with Test_F() method.
So, I did lots of analysis, but still did not find any solution.
Please help me.
所以,如果我会看到 GoogleTest,有一个方法叫做test_case_to_run_count()
; 但是所有这些test cases
都使用 Test_F() 方法注册。所以,我做了很多分析,但仍然没有找到任何解决方案。请帮我。
回答by nogard
You could use advanced optionsto run Google tests.
您可以使用高级选项来运行 Google 测试。
To run only some unit tests you could use --gtest_filter=Test_Cases1*
command line option with value that accepts the *
and ?
wildcards for matching with multiple tests. I think it will solve your problem.
要仅运行某些单元测试,您可以使用--gtest_filter=Test_Cases1*
命令行选项,其值接受*
和?
通配符以匹配多个测试。我认为它会解决你的问题。
UPD:
更新:
Well, the question was how to runspecific test cases. Integration of gtest with your GUI is another thing, which I can't really comment, because you didn't provide details of your approach. However I believe the following approach might be a good start:
那么,问题是如何运行特定的测试用例。将 gtest 与您的 GUI 集成是另一回事,我无法真正评论,因为您没有提供您的方法的详细信息。但是我相信以下方法可能是一个好的开始:
- Get all testcases by running tests with
--gtest_list_tests
- Parse this data into your GUI
- Select test cases you want ro run
- Run test executable with option
--gtest_filter
- 通过运行测试来获取所有测试用例
--gtest_list_tests
- 将此数据解析到您的 GUI 中
- 选择你想要运行的测试用例
- 使用选项运行测试可执行文件
--gtest_filter
回答by Jorge Leitao
Summarising @Rasmi Ranjan Nayak and @nogard answers and adding another option:
总结@Rasmi Ranjan Nayak 和@nogard 的答案并添加另一个选项:
On the console
在控制台上
You should use the flag --gtest_filter
, like
你应该使用标志--gtest_filter
,比如
--gtest_filter=Test_Cases1*
(You can also do this in Properties|Configuration Properties|Debugging|Command Arguments)
(您也可以在属性|配置属性|调试|命令参数中执行此操作)
On the environment
关于环境
You should set the variable GTEST_FILTER
like
你应该GTEST_FILTER
像这样设置变量
export GTEST_FILTER = "Test_Cases1*"
On the code
在代码上
You should set a flag filter
, like
你应该设置一个标志filter
,比如
::testing::GTEST_FLAG(filter) = "Test_Cases1*";
such that your main function becomes something like
这样你的主要功能就变得像
int main(int argc, char **argv) {
::testing::InitGoogleTest(&argc, argv);
::testing::GTEST_FLAG(filter) = "Test_Cases1*";
return RUN_ALL_TESTS();
}
See section Running a Subset of the Testsfor more info on the syntax of the string you can use.
有关您可以使用的字符串语法的更多信息,请参阅运行测试的子集部分。
回答by Rasmi Ranjan Nayak
Finally I got some answer,
::test::GTEST_FLAG(list_tests) = true;
//From your program, not w.r.t console.
最后我得到了一些答案,
::test::GTEST_FLAG(list_tests) = true;
//来自你的程序,而不是控制台。
If you would like to use --gtest_filter =*;
/* =*, =xyz*... etc*/
// You need to use them in Console.
如果您想使用--gtest_filter =*;
/* =*, =xyz*... etc*/
// 您需要在控制台中使用它们。
So, my requirement is to use them from the program not from the console.
所以,我的要求是从程序而不是从控制台使用它们。
Updated:-
Updated:-
Finally I got the answer for updating the same in from the program.
最后我得到了从程序中更新相同内容的答案。
::testing::GTEST_FLAG(filter) = "*Counter*:*IsPrime*:*ListenersTest.DoesNotLeak*";//":-:*Counter*";
InitGoogleTest(&argc, argv);
RUN_ALL_TEST();
So, Thanks for all the answers.
所以,感谢所有的答案。
You people are great.
你们人真好。