通过eventlist 模拟多个event,测试widget 对event的处理情况。
class TestGui: public QObject
{
Q_OBJECT
private slots:
void testGui_data();
void testGui();
};
和之前测试function的方式一样,创建testcase 和 添加数据函数。
void TestGui::testGui_data()
{
QTest::addColumn<QTestEventList>("events");
QTest::addColumn<QString>("expected");
// 通过QTestEventList 保存模拟的输入按键
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);
}
处理一组输入按键,简化测试程序。
本文介绍如何使用Qt Testlib库来测试GUI组件对不同事件的响应,特别是通过模拟多个事件来检查Widget对象的处理能力。测试过程中,创建了QObject类型的TestGui类,并定义了数据提供函数testGui_data。
&spm=1001.2101.3001.5002&articleId=17731045&d=1&t=3&u=8187069468fd408fa3df1960b6b424a9)
1312

被折叠的 条评论
为什么被折叠?



