如何使用 QCOMPARE 宏来比较事件

发布于 2024-08-30 01:28:52 字数 985 浏览 10 评论 0原文

我有 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 技术交流群。

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

发布评论

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

评论(1

給妳壹絲溫柔 2024-09-06 01:28:52

查看 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.

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