利用google test 与QTest进行GUI 测试
Jingwenlai 2009-11-10
Google test写测试用例非常方便,而且相对于QTest来说,提供了更多的比较宏,但其并未提供测试GUI的方法,相反,Qt Test lib虽然仅提供了QCOMPARE等少数的宏,但QtTest提供了诸如keyClick,keyClicks,mouseClicks,mouseClick等模拟UI行为的测试方法。可以将两者结合起来对QT的GUI程序进行测试,同时,也能够利用Google Test的便利性来写测试程序。 please refer to QtAssitant.exe for more informations about QtTest lib.
下面是一个示例程序
这个示例完成如下功能,根据用户的输入对显示的值进行增减。
执行了五个测试,其中一个是初始化测试,另外两个是正常情况的测试,还有两种情况是极端情况的测试。
以下是测试代码/**Testing Code;*/
#ifndef TEST_PANELITEM_HPP
#define TEST_PANELITEM_HPP
#include <gtest/gtest.h>
#include "PanelItem.h"
#include <QtTest/QtTest> //for testing Gui
#include <QString>
class GPanelItemTest : public ::testing::Test
{
protected:
virtual void SetUp()
{
panelItem = new GPanelItem();
}
virtual void TearDown()
{
delete panelItem;
}
GPanelItem * panelItem;
};
TEST_F(GPanelItemTest,testInit)
{
}
//GUI testing using QtTest
TEST_F(GPanelItemTest,testIncButton)
{
panelItem->reset();
QTest::mouseClick(panelItem->getIncreaseButton(),Qt::LeftButton);
QCOMPARE(panelItem->getNumLabel()->text(),QString("1"));
}
TEST_F(GPanelItemTest,testDescButton)
{
panelItem->setValue(1);
QCOMPARE(panelItem->getNumLabel()->text(),QString("1"));
QTest::mouseClick(panelItem->getDecreaseButton(),Qt::LeftButton);
QCOMPARE(panelItem->getNumLabel()->text(),QString("0"));
}
TEST_F(GPanelItemTest,testIncButtonEdge)
{
panelItem->setValue(9);
QTest::mouseClick(panelItem->getIncreaseButton(),Qt::LeftButton);
QCOMPARE(panelItem->getNumLabel()->text(),QString("9"));
}
TEST_F(GPanelItemTest,testDescButtonEdge)
{
panelItem->setValue(0);
QTest::mouseClick(panelItem->getDecreaseButton(),Qt::LeftButton);
QCOMPARE(panelItem->getNumLabel()->text(),QString("0"));
}
#endif
//PenalItem 类:
#ifndef PANELITEM_H
#define PANELITEM_H
#include <QWidget>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QPushButton>
#include <QLabel>
#include <QObject>
class GPanelItem : public QWidget
{
Q_OBJECT
public:
GPanelItem(QWidget * parent = 0) : QWidget(parent)
{
increaseButton = new QPushButton(QString(tr("<")),this);
QObject::connect(increaseButton,SIGNAL(clicked()),this,SLOT(increaseNum()));
decreaseButton = new QPushButton(QString(tr(">")),this);
QObject::connect(decreaseButton,SIGNAL(clicked()),this,SLOT(decreaseNum()));
value = 0;
numLabel = new QLabel();
numLabel->setNum(value);
}
~GPanelItem()
{
delete increaseButton;
delete decreaseButton;
delete numLabel;
}
public:
void reset()
{
setValue(0);
}
void setValue(int value)
{
this->value = value;
numLabel->setNum(value);
}
private slots:
void increaseNum()
{
if(value < 9)
value++;
numLabel->setNum(value);
}
void decreaseNum()
{
if(value > 0)
value--;
numLabel->setNum(value);
}
public:
QPushButton * getIncreaseButton() { return increaseButton; }
QPushButton * getDecreaseButton() { return decreaseButton; }
QLabel * getNumLabel() { return numLabel; }
private:
QPushButton* increaseButton;
QPushButton* decreaseButton;
QLabel * numLabel;
private:
int value;
};
#endif
//主程序
#include <QApplication>
#include "PanelItem.hpp"
#include <gtest/gtest.h>
#include "test.hpp"
int main(int argc,char ** argv)
{
QApplication app(argc,argv);
::testing::InitGoogleTest(&argc,argv);
RUN_ALL_TESTS();
GPanelItem * panelItem = new GPanelItem();
panelItem->show();
return app.exec();
}
对应的CMakeLists.txt 如下:
project(QViewpad)
cmake_minimum_required(VERSION 2.6)
set(SOURCES main.cpp PanelItem.cpp)
set(HEADERS PanelItem.hpp)
find_package(Qt4 COMPONENTS QtTest QtGui QtCore REQUIRED)
if(QT4_FOUND)
include(${QT_USE_FILE})
endif()
qt4_wrap_cpp(GPanelItem_Moc PanelItem.hpp)
add_executable(viewpad ${SOURCES} ${GPanelItem_MOC})
target_link_libraries(viewpad ${QT_LIBRARIES} gtest gtest_main)