我如何在基于消息队列的Windows应用程序上使用谷歌测试?

发布于 2024-12-01 09:15:25 字数 635 浏览 2 评论 0原文

我想对我的程序使用谷歌测试,该程序内部有计时器功能。 定时器是通过windows SetTimer()实现的,main()中有一个消息队列来处理超时消息。

while (GetMessage(&msg, NULL, 0, 0)) {
    if (msg.message == WM_TIMER) {
        ...
    }
    DispatchMessage(&msg);
}

对于 google 测试,它调用 RUN_ALL_TESTS() 来开始测试。

int main( int argc , char *argv[] )
{
    testing::InitGoogleTest( &argc , argv );
    return RUN_ALL_TESTS();
}

我的问题是如何整合这两部分。因为我的代码的某些函数会发送消息,所以我应该有相同的消息队列机制来处理它。

这是否意味着我需要在每个测试用例中编写消息队列处理?这是一个可行的方法吗?

TEST()
{
    ... message queue here ...
}

有没有合适的方法来进行这种测试? 谢谢大家。

I want to use google test for my program that has timer functionality inside.
The timer is implement by windows SetTimer(), and there is a message queue in the main() to process the timeout message.

while (GetMessage(&msg, NULL, 0, 0)) {
    if (msg.message == WM_TIMER) {
        ...
    }
    DispatchMessage(&msg);
}

For the google test, it calls RUN_ALL_TESTS() to start the testing.

int main( int argc , char *argv[] )
{
    testing::InitGoogleTest( &argc , argv );
    return RUN_ALL_TESTS();
}

My question is that how could I integrate these two part. Because some function of my code will send out a message, I should have the same message queue mechanism to handle it.

Does that means I need to write the message queue handling in each test case? Is it a workable method?

TEST()
{
    ... message queue here ...
}

Is there any proper method to do this kind of test?
Thank you all.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

讽刺将军 2024-12-08 09:15:26

看来您要测试的代码依赖于消息队列机制。如果您实现一个像这样的抽象消息处理程序类,将其注入到需要发送消息的每个类中,则可以提高可测试性:

class MessageHandler
{
  virtual void DispatchMessage(Msg messageToBeDispatched) = 0;
}

现在您可以实现不同的消息处理程序以实现生产和测试目的:

class TestMessageHandler : public MessageHandler
{
  void DispatchMessage(Msg messageToBeDispatched)
  {
     // Just testing, do nothing with this message, or just cout...
  }
}

class ProductiveMessageHandler : public MessageHandler
{
  void DispatchMessage(Msg messageToBeDispatched)
  {
     // Now do the real thing
  }
}

在您的代码中,您现在可以注入'ProductiveMessageHandler' 或 'TestMessageHandler',或者您甚至可以使用 GoogleMock 的模拟测试处理程序来测试期望。

class MyProductionCode
{
   MyProductionCode(MessageHandler *useThisInjectedMessageHandler);
}

你的测试代码看起来像这样:

class TestMyProductionCode : public ::testing::Test
{
  TestMessageHandler myTestMessageHandler;
}

TEST(TestMyProductionCode, ExampleTest)
{
  MyProductionCode myTestClass(&myTestMessageHandler);

  ASSERT_TRUE(myTestClass.myTestFunction());
}

It seems that the code you want to test is dependent of a message queue mechanismn. You could improve testability, if you implement an abstract message handler class like this that gets injected into every class that needs to send a message:

class MessageHandler
{
  virtual void DispatchMessage(Msg messageToBeDispatched) = 0;
}

Now you could implement different message handlers for productiv and for testing purpose:

class TestMessageHandler : public MessageHandler
{
  void DispatchMessage(Msg messageToBeDispatched)
  {
     // Just testing, do nothing with this message, or just cout...
  }
}

class ProductiveMessageHandler : public MessageHandler
{
  void DispatchMessage(Msg messageToBeDispatched)
  {
     // Now do the real thing
  }
}

In your code you could now inject either a 'ProductiveMessageHandler' or a 'TestMessageHandler', or you could even use a mocked test handler using GoogleMock to test expectations.

class MyProductionCode
{
   MyProductionCode(MessageHandler *useThisInjectedMessageHandler);
}

Your testcode looks like that:

class TestMyProductionCode : public ::testing::Test
{
  TestMessageHandler myTestMessageHandler;
}

TEST(TestMyProductionCode, ExampleTest)
{
  MyProductionCode myTestClass(&myTestMessageHandler);

  ASSERT_TRUE(myTestClass.myTestFunction());
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文