简体   繁体   English

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

[英]How to run two different tests in Googletest

Suppose I have two/many different tests need to be carried out in gtest in two iteration. 假设我有两个/很多不同的测试需要在gtest中进行两次迭代。 So, how to carryout the same? 那么,如何进行相同? I tried my approch but it fails. 我尝试了方法,但失败了。 I wrote, 我写,

::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............

But Google test is taking only the "*first*:*second*" and runs two times. 但是Google测试仅采用"*first*:*second*"并且运行两次。 Please help me. 请帮我。 My reqiurement is Gtest should run all the test cases one by one. 我的要求是Gtest应该一个一个地运行所有测试用例。 eg first it will execute case 1: then case 2: and so on... 例如,首先将执行case 1:然后执行case 1: case 2:依此类推...

I don't think you can do this using ::testing::GTEST_FLAG(repeat) 我认为您不能使用::testing::GTEST_FLAG(repeat)来执行此操作

However, you could achieve your goal with something like: 但是,您可以通过以下方式实现目标:

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

I'm not sure how gtest calculates the return value of RUN_ALL_TESTS() when GTEST_FLAG(repeat) is used, but here main will return 0 if all tests pass, otherwise it will return the last non-zero value of the RUN_ALL_TESTS() invocations. 我不确定使用GTEST_FLAG(repeat) RUN_ALL_TESTS()GTEST_FLAG(repeat)如何计算RUN_ALL_TESTS()的返回值,但是如果所有测试通过,则main将返回0 ,否则它将返回RUN_ALL_TESTS()调用的最后一个非零值。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM