1、.pro文件增加QT += testlib
QT工程文件qttest.pro
QT += core testlib
TARGET = QTTest CONFIG += console
TEMPLATE = app
QMAKE_CXXFLAGS += --coverage LIBS += -lgcov
DESTDIR += ./
SOURCES += main.cpp TestCase.cpp
HEADERS += TestCase.h
INCLUDEPATH+= ./
2、main.cpp 包含头文件#include<QtTest/QtTest>
主程序main.cpp
#include <QtCore/QCoreApplication> #include <QtTest/QtTest> #include <QDebug> #include "TestCase.h"
int main(int argc, char *argv[]) { qDebug() << "Test start"; QCoreApplication a(argc, argv);
CTestCase TestCase; QTest::qExec(&TestCase);
qDebug() << "Test end"; return 0; }
3、编写测试用例类
测试用例类头文件TestCase.h
#ifndef TEST_CASE_H #define TEST_CASE_H #include <QtTest/QtTest>
class CTestCase:public QObject { Q_OBJECT public: CTestCase(); virtual ~CTestCase();
private Q_SLOTS: //测试用例1 void Test_Case1(); //测试用例2 void Test_Case2(); private : int m_iRtn; //整形返回值 };
#endif //TEST_CASE_H
测试用例类实现 TestCase.cpp
#include "TestCase.h"
CTestCase::CTestCase() { }
CTestCase::~CTestCase() { }
void CTestCase::Test_Case1() { //m_iRtn = test1(); //如果被测试函数返回为0,则认为用例通过。 QVERIFY(0==m_iRtn); }
void CTestCase::Test_Case2() { //m_iRtn = test2();
//如果被测试函数返回为1,则认为用例通过。 QVERIFY(1==m_iRtn); }