如何在windows应用程序上使用基于消息队列的谷歌测试
How could I use google test on windows application based on message queue?
我想使用谷歌测试我的程序,里面有定时器功能。定时器由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 ...
}
有什么合适的方法来做这种测试吗?谢谢大家。
您想要测试的代码似乎依赖于消息队列机制。你可以提高可测试性,如果你实现一个抽象的消息处理程序类,像这样被注入到每一个需要发送消息的类:
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);
}
你的testcode看起来像这样:
class TestMyProductionCode : public ::testing::Test
{
TestMessageHandler myTestMessageHandler;
}
TEST(TestMyProductionCode, ExampleTest)
{
MyProductionCode myTestClass(&myTestMessageHandler);
ASSERT_TRUE(myTestClass.myTestFunction());
}
相关文章:
- boost::进程间消息队列引发错误
- 避免使用 boost::进程间::消息队列创建文件
- 提升消息队列 跨两个进程未接收
- 可以将Boost消息队列文件重定向到用户指定的位置
- 使用加速进程间创建消息队列 - 内存访问冲突
- 如何检查提升消息队列是否存在
- Win32 消息队列在使用 OpenGL 渲染时被淹没
- 在尝试使用boost时断言.跨很多过程中的互动消息队列
- GetMessage/PeekMessage - 删除消息队列中的所有消息
- IPC Unix 消息队列线程安全吗?
- 在.c文件接收函数中使用Linux中的MSGGET创建消息队列未实现错误
- boost消息队列线程安全和进程安全吗?
- 为什么我的无锁消息队列段错误:(?
- 在控制台应用程序中处理空的windows消息队列
- 关于在这种情况下消息队列与共享内存的适用性或适用性
- boost::进程间消息队列创建时的竞争条件
- 从内存转储中查找线程消息队列中的消息
- win32消息泵,do dispatchMessage()处理整个消息队列或仅仅是顶部消息
- 如何使用Boost进程间消息队列for Windows
- 来自另一个线程的 SendMessage() 调用是否将消息发布到消息队列