1

I am new to gtest. My job is related to hardware. When I was writting my testcases, I am wondering if there is a way that does some prechecks before each test and make result of the test successful if the precheck result is false.

For example, I am testing one of the modules I developed, it turns out that for this board, it does not even support the module. There is really no reason to continue run the following test codes. Is there a way that I can make all the test just return success? Since I have many test cases, call the check function in every testcase is very repetitive. Does gtest have some special features for this problem? I tried SetUp() like

class Module : public testing::Test{
  protected:
  void SetUp() override{
    get_status = (a function i wrote to check if the board supports the module)
    if (!get_status) {
      SUCCEED();
    }
  }
  unsigned int get_status;
};

But it seems not working. Can anyone tell me how to do this or show me a right example? Thank you!

Evg
  • 25,259
  • 5
  • 41
  • 83

1 Answers1

3

Adapted from this answer.

You can try this:

class Module : public testing::Test{
 protected:
  void SetUp() override{
    bool get_status = true;
    if (get_status) {
      std::cout << "Skipped!" << std::endl;
      GTEST_SKIP();
    }
  }
  unsigned int get_status;
};

TEST_F(Module, SkipThisTest) {
  FAIL(); // This will always fail
}

Output will be: Test skipped example

Jan Gabriel
  • 1,066
  • 6
  • 15