如何使用 QCOMPARE 宏来比较事件
我有 MyWindow 类,它会弹出一个空白窗口,该窗口接受鼠标单击,我需要对鼠标单击事件进行单元测试
代码片段:
void TestGui::testGUI_data()
{
QTest::addColumn<QTestEventList>("events");
QTest::addColumn<QTestEventList>("expected");
Mywindow mywindow;
QSize editWidgetSize = mywindow.size();
QPoint clickPoint(editWidgetSize.rwidth()-2, editWidgetSize.rheight()-2);
QTestEventList events, expected;
events.addMouseClick( Qt::LeftButton, 0, clickPoint);
expected.addMouseClick( Qt::LeftButton, 0, clickPoint);
QTest::newRow("mouseclick") << events << expected ;
}
void TestGui::testGUI()
{
QFETCH(QTestEventList, events);
QFETCH(QTestEventList, expected);
Mywindow mywindow;
mywindow.show();
events.simulate(&mywindow);
QCOMPARE(events, expected); } // prints FAIL! : TestGui::testGUI(mouseclick) Compared values are not the same
...
}
如何测试 mywindow
上的鼠标单击>。有没有更好的方法来单元测试鼠标事件?
谢谢, 韦尔斯
I have MyWindow class which pops up a blank window, which accepts a mouse click, I need to unit test the mouse click event
Code snippet:
void TestGui::testGUI_data()
{
QTest::addColumn<QTestEventList>("events");
QTest::addColumn<QTestEventList>("expected");
Mywindow mywindow;
QSize editWidgetSize = mywindow.size();
QPoint clickPoint(editWidgetSize.rwidth()-2, editWidgetSize.rheight()-2);
QTestEventList events, expected;
events.addMouseClick( Qt::LeftButton, 0, clickPoint);
expected.addMouseClick( Qt::LeftButton, 0, clickPoint);
QTest::newRow("mouseclick") << events << expected ;
}
void TestGui::testGUI()
{
QFETCH(QTestEventList, events);
QFETCH(QTestEventList, expected);
Mywindow mywindow;
mywindow.show();
events.simulate(&mywindow);
QCOMPARE(events, expected); } // prints FAIL! : TestGui::testGUI(mouseclick) Compared values are not the same
...
}
How to test the mouse click on mywindow
. is there any better approach to unit test mouse events?
Thanks,
vels
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
查看 QTest 命名空间文档。 QTest::mouseClick 函数允许您模拟鼠标单击小部件的给定位置(甚至不需要显示小部件)。
据我记得 QCOMPARE 使用“operator==”作为给定的参数,如果它返回 false,那么它会尝试使用“QTest::toString(const T& t)”函数来打印它。如果没有 QCOMPARE 参数类型的实现,那么它只会打印值不同。再次查看 QTest 命名空间文档,了解如何为您需要的类型重新实现“QTest::toString(const T& t)”的详细信息。
Take a look on QTest namespace documentation. There is QTest::mouseClick function which allows you to emulate mouse click on the given position of your widget (there is even no necessity to show the widget).
As far as I remember QCOMPARE uses "operator==" for the arguments given and if it returns false then it tries to print it using "QTest::toString<T>(const T& t)" function. If there is no implementation for a type of QCOMPARE arguments then it will just print that values are different. Again take a look on the QTest namespace documentation for details how to reimplement "QTest::toString<T> (const T& t)" for the type you need.