4

假设我有两个/许多不同的测试需要在两次迭代中在 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............

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

4

2 回答 2

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;
}

我不确定 gtest 如何计算使用RUN_ALL_TESTS()时的返回值GTEST_FLAG(repeat),但是如果所有测试都通过,这里main将返回,否则它将返回调用0的最后一个非零值。RUN_ALL_TESTS()

于 2012-08-28T02:33:23.113 回答
3
int main(int argc, char **argv) {
    int i = 1;
        vector<string> str;
        str.push_back("*first*:*second*");
        str.push_back("*third*:*fourth*");
        str.push_back("*fifth.fifthtestname*");
        for(i = 0; i != str.size(); i++)
        {
            ::testing::GTEST_FLAG(filter) = str.at(i);
             InitGoogleTest(&argc, argv);
             RUN_ALL_TESTS();
            // getchar();
        }
        getchar();
}
于 2012-08-29T06:55:29.637 回答