Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. 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. Please help me. My reqiurement is Gtest should run all the test cases one by one. e.g first it will execute case 1: then case 2: and so on...

like image 431
Rasmi Ranjan Nayak Avatar asked Feb 19 '23 09:02

Rasmi Ranjan Nayak


1 Answers

I don't think you can do this using ::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.

like image 191
Fraser Avatar answered Feb 22 '23 00:02

Fraser