Qt Testlib 测试GUI(1)

Qt Testlib 提供一种机制模拟native的event,发送event到GUI的widget上,测试widget对event的响应。

测试QLineEdit

 classTestGui:public QObject

 {
     Q_OBJECT
 
  
 private slots:
     void testGui();
 
  
 };
 
  
 void TestGui::testGui()
 {
     QLineEdit lineEdit;
 
     // 模拟key clicks ,为QLineEdit 输入 "hello world"
     QTest::keyClicks(&lineEdit, "hello world");
 
  
     QCOMPARE(lineEdit.text(), QString("hello world"));
 }
这个测试用例测试QLineEdit的 text 函数对key click的处理是否正常。
 
QTest 提供了多个模拟函数
QTest::keyClicks() 模拟一系列的键盘输入
KeyClick() 模拟一个键盘输入
KeyPress() 模拟键盘按键按下
KeyRelease() 模拟键盘按键弹起
mouseClick() 模拟鼠标单击
mouseDClick()模拟鼠标双击
mouseMove() 模拟鼠标移动
mousePress() 模拟鼠标按下
mouseRelease() 模拟鼠标释放
 
通过以上的函数和参数可以模拟大部分GUI输入信息。
 

 

 

你可能感兴趣的:(Qt,Test)