如何在 Googletest 中运行两个不同的测试

Ras*_*yak 4 c++ googletest visual-studio-2010 visual-c++

假设我有两个/许多不同的测试需要在两次迭代中在 gtest 中进行。那么,如何进行相同的呢?我尝试了我的方法,但失败了。我写,

::testing::GTEST_FLAG(repeat) = 2; //may be 2 or 3 or so on...
switch(i) //int i = 1;
{
case 1:
::testing::GTEST_FLAG(filter) = "*first*:*second*";
i++; break;
case 2:
::testing::GTEST_FLAG(filter) = "*third*:*fourth*";
i++; break;
and so on............
Run Code Online (Sandbox Code Playgroud)

但谷歌测试只需要"*first*:*second*"和 运行两次。请帮我。我的要求是 Gtest 应该一一运行所有的测试用例。例如首先它会执行case 1:然后case 2:等等......

Fra*_*ser 5

我不认为你可以使用 ::testing::GTEST_FLAG(repeat)

但是,您可以通过以下方式实现您的目标:

#include "gtest/gtest.h"

int RunTests(int iteration) {
  switch(iteration) {
    case 1:  ::testing::GTEST_FLAG(filter) = "*first*:*second*"; break;
    case 2:  ::testing::GTEST_FLAG(filter) = "*third*:*fourth*"; break;
    default: ::testing::GTEST_FLAG(filter) = "*";
  }
  return RUN_ALL_TESTS();
}

int main(int argc, char **argv) {
  ::testing::InitGoogleTest(&argc, argv);
  int final_result(0);
  for (int i(0); i < 3; ++i) {
    int result(RunTests(i));
    if (result != 0)
      final_result = result;
  }
  return final_result;
}
Run Code Online (Sandbox Code Playgroud)

我不知道GTEST如何计算的返回值RUN_ALL_TESTS()时GTEST_FLAG(repeat)使用,但在这里main将返回0如果通过了所有测试,否则将返回的最后一个非零值RUN_ALL_TESTS()调用。