关闭

Qt单元测试框架

发表于:2013-7-23 11:38

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:newsyoung1    来源:51Testing软件测试网采编

  为了测试GUI组件 ,可以使用诸如QTest::keyClick()函数,通过内部事件传递,来模拟本地窗口系统的事件。例如:

// 代码取自 http://qt.nokia.com/doc/qtestlib-tutorial4-testgui-cpp.html
#include <QtGui>
#include <QtTest/QtTest>
class TestGui: public QObject
{
  Q_OBJECT
private slots:
  void testGui_data();
  void testGui();
};
void TestGui::testGui_data()
{
  QTest::addColumn<QTestEventList>("events");
  QTest::addColumn<QString>("expected");
  // 添加按键事件
  QTestEventList list1;
  list1.addKeyClick('a');
  QTest::newRow("char") << list1 << "a";
  QTestEventList list2;
  list2.addKeyClick('a');
  list2.addKeyClick(Qt::Key_Backspace);
  QTest::newRow("there and back again") << list2 << "";
}
void TestGui::testGui()
{
  QFETCH(QTestEventList, events);
  QFETCH(QString, expected);
  QLineEdit lineEdit;
  // 模拟按键事件,并比较结果
  events.simulate(&lineEdit);
  QCOMPARE(lineEdit.text(), expected);
}
QTEST_MAIN(TestGui)
#include "testgui.moc"

  最后看看如何测试benchmark

void TestQString::testBenchmark()
{
  QString str("HeLlO");
  // 以下代码被测试benchmark
  QBENCHMARK
  {
    str.toLower();
  }
}

  得到如下输出:

RESULT : TestQString::testBenchmark():
0.00062 msec per iteration (total: 41, iterations: 65536)
22/2<12
《2023软件测试行业现状调查报告》独家发布~

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计 发展历程

法律顾问:上海兰迪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2024
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号