简体   繁体   English

如何在执行 GoogleTest 时跳过源代码中的“代码”部分

[英]How to Skip a Portion of the ”Code” in the source code while doing GoogleTest

I tried using a debug flag addition ( GOOGLE_TEST ) in the source code and defined it in the TEST/Makefile.am.我尝试在源代码中使用调试标志添加 ( GOOGLE_TEST ) 并在 TEST/Makefile.am 中定义它。 but the things didn't work.但事情没有奏效。 I am using C++ Language.我正在使用 C++ 语言。 Note: I don't want to change anything in the SRC Directory code which will affect the production code and its Makefile.am注意:我不想更改 SRC 目录代码中的任何会影响生产代码及其 Makefile.am 的内容

Test Class in SRC Directory SRC 目录中的测试类

class Common: public Thread {
 public:
  friend class test_common;
  Common {
  }
  ~Common () {
  }
  virtual void ThreadMain();
 protected:
  virtual void ProcessData(void);
};
void Common::ProcessData(void) {
  #ifndef __GOOGLE_TEST__
  while (1) { }
  #endif
}

TESTCODE in test Directory测试目录中的 TESTCODE

class test_common : public ::testing::Test {
};
TEST_F(test_common, create_common) {
    Common commonObj();
    commonObj. ProcessData ();
}

OUTPUT输出

GTest Stuck in the While loop part even after defining the flag in the test/makefile.am即使在 test/makefile.am 中定义了标志后,GTest 仍停留在 While 循环部分

Dont rely on the compilation flags, without affecting the production code use the GMOCK methods to get rid off the while (1) loop , the code can go like below:不要依赖编译标志,在不影响生产代码的情况下使用 GMOCK 方法摆脱 while (1) 循环,代码可以如下所示:

TESTCODE:测试代码:

class test_common : public ::testing::Test {
};
TEST_F(test_common, create_common) {
    Common commonObj();
    ON_CALL(mock_if, GetBool())
      .WillByDefault(Return(true));
    EXPECT_CALL(mock_if, GetBool())
      .Times(AtLeast(1))
      .WillOnce(Return(true))
      .WillOnce(Return(false));
    commonObj. ProcessData ();
}

ABSTRACT CODE:抽象代码:

class AbstractIf {
 public:
  AbstractIf (void) = default;
  virtual ~AbstractIf (void) = default;
  virtual bool GetBool() = 0;
};

MOCK CODE:模拟代码:

class MockIf : public AbstractIf {
  public:
    MOCK_METHOD0(GetBool,bool());
};

SOURCE CODE:源代码:

class Common: public Thread {
 public:
  friend class test_common;
  Common {
  }
  ~Common () {
  }
  virtual void ThreadMain();
 protected:
  virtual void ProcessData(void);
  AbstractIf *prov_fl_if_;
};
void Common::ProcessData(void) {
  while (prov_fl_if_->GetBool())  { }
}

By this way we can skip the part of the code we want without affecting the production code这样我们就可以在不影响生产代码的情况下跳过我们想要的那部分代码

There is no way to make a #define value from one compilation unit affect the compilation of another, previously compiled units.无法使一个编译单元的#define值影响另一个先前编译单元的编译。 Given the list of things you have stipulated you don't want to do, you will have to use some form of runtime shenanigans.鉴于您已规定不想做的事情列表,您将不得不使用某种形式的运行时恶作剧。

You could make ProcessData take an argument that determines whether the loop should iterate:您可以让 ProcessData 接受一个参数来确定循环是否应该迭代:

void ProcessData(bool once=false);

void Common::ProcessData(bool once) {
    do {
    // ... your loop code
    } while (!once);
}

Or you could use a global variable that is defined in the module with your main() in it, lets call it main.cpp .或者您可以使用在模块中定义的全局变量,并在其中使用main() ,我们称之为main.cpp

Production main.cpp :生产main.cpp :

const bool g_isDebugMode = false;

Unit test main.cpp :单元测试main.cpp

const bool g_isDebugMode = true;

main.h

extern const bool g_isDebugMode;

now you can write runtime tests against this variable.现在您可以针对此变量编写运行时测试。

do {
// your code
} while (g_isDebugMode == false);

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

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