我如何在基于消息队列的Windows应用程序上使用谷歌测试?
我想对我的程序使用谷歌测试,该程序内部有计时器功能。 定时器是通过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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看来您要测试的代码依赖于消息队列机制。如果您实现一个像这样的抽象消息处理程序类,将其注入到需要发送消息的每个类中,则可以提高可测试性:
现在您可以实现不同的消息处理程序以实现生产和测试目的:
在您的代码中,您现在可以注入'ProductiveMessageHandler' 或 'TestMessageHandler',或者您甚至可以使用 GoogleMock 的模拟测试处理程序来测试期望。
你的测试代码看起来像这样:
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:
Now you could implement different message handlers for productiv and for testing purpose:
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.
Your testcode looks like that: