如何在GoogleTest中运行特定的测试用例?

时间:2023-02-09 01:21:00

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到…在result窗口中,我还希望看到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_F()方法注册的。所以,我做了很多分析,但仍然没有找到任何解决方案。请帮助我。

3 个解决方案

#1


117  

You could use advanced options to run Google tests.

您可以使用高级选项来运行谷歌测试。

To run only some unit tests you could use --gtest_filter=Test_Cases1* command line option with value which is regular expression. I think it will solve your problem.

要只运行可以使用的单元测试——gtest_filter=Test_Cases1*命令行选项,值为正则表达式。我认为这将解决你的问题。

UPD:

乌利希期刊指南:

Well, the question was how to run specific 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的集成是另一件事,我无法评论,因为您没有提供您的方法的细节。然而,我认为以下方法可能是一个好的开始:

  1. Get all testcases by running tests with --gtest_list_tests
  2. 通过使用—gtest_list_tests运行测试来获取所有测试用例
  3. Parse this data into your GUI
  4. 将这些数据解析到GUI中
  5. Select test cases you want ro run
  6. 选择您希望ro运行的测试用例
  7. Run test executable with option --gtest_filter
  8. 使用选项-gtest_filter运行测试可执行文件。

#2


43  

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

您应该设置一个标志过滤器,比如

::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 Tests for more info on the syntax of the string you can use.

参见部分运行测试的一个子集,以获得关于您可以使用的字符串的语法的更多信息。

#3


22  

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;//从你的程序,而不是w.r。t控制台。

If you would like to use --gtest_filter =*; /* =*, =xyz*... etc*/ // You need to use them in Console.

如果您想使用——gtest_filter =*;/ * = * = xyz *……你需要在控制台使用它们。

So, my requirement is to use them from the program not from the console.

所以,我的要求是从程序而不是控制台来使用它们。

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.

你人很好。

#1


117  

You could use advanced options to run Google tests.

您可以使用高级选项来运行谷歌测试。

To run only some unit tests you could use --gtest_filter=Test_Cases1* command line option with value which is regular expression. I think it will solve your problem.

要只运行可以使用的单元测试——gtest_filter=Test_Cases1*命令行选项,值为正则表达式。我认为这将解决你的问题。

UPD:

乌利希期刊指南:

Well, the question was how to run specific 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的集成是另一件事,我无法评论,因为您没有提供您的方法的细节。然而,我认为以下方法可能是一个好的开始:

  1. Get all testcases by running tests with --gtest_list_tests
  2. 通过使用—gtest_list_tests运行测试来获取所有测试用例
  3. Parse this data into your GUI
  4. 将这些数据解析到GUI中
  5. Select test cases you want ro run
  6. 选择您希望ro运行的测试用例
  7. Run test executable with option --gtest_filter
  8. 使用选项-gtest_filter运行测试可执行文件。

#2


43  

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

您应该设置一个标志过滤器,比如

::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 Tests for more info on the syntax of the string you can use.

参见部分运行测试的一个子集,以获得关于您可以使用的字符串的语法的更多信息。

#3


22  

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;//从你的程序,而不是w.r。t控制台。

If you would like to use --gtest_filter =*; /* =*, =xyz*... etc*/ // You need to use them in Console.

如果您想使用——gtest_filter =*;/ * = * = xyz *……你需要在控制台使用它们。

So, my requirement is to use them from the program not from the console.

所以,我的要求是从程序而不是控制台来使用它们。

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.

你人很好。