单元测试就是对每个程序的一些功能函数进行效果测试,比较经典的就是Google test
,可以看这个链接学习:Linux下Google Test (GTest)测试环境搭建步骤. 有关makefile
的东西可以看Makefile经典教程(掌握这些足够)学习。
由于我使用的是Qt,所以我对Qt自己的单元测试进行一个学习和记录。
Qt单元测试框架
1 .pro和include
首先新建一个工程,在.pro中添加
QT += testlib
可以写成这样:
WHU_server.pro
QT += core testlib
QT -= gui
CONFIG += c++11
TARGET = WHU_server
CONFIG += console
CONFIG -= app_bundle
TEMPLATE = app
SOURCES += main.cpp \
test_union.cpp
# The following define makes your compiler emit warnings if you use
# any feature of Qt which as been marked deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS
# You can also make your code fail to compile if you use deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
HEADERS += \
test_union.h
新建一个c++
的头文件和c文件,头文件需要包含#include <QtTest/QtTest>
,必须继承QObject
。
2 测试程序
单元测试会自动运行private slots:
属性的函数,还有四个内置的测试函数会在特定的时候被调用:
initTestCase():在测试开始前被调用
cleanupTestCase():在测试结束后被调用
init():每个测试函数执行前被调用
cleanup():每个测试函数执行后被调用
测试程序如下:
test_union.h
#ifndef TEST_UNION_H
#define TEST_UNION_H
#include <QObject>
#include <QtTest/QtTest>
#include <QDebug>
class test_union : public QObject
{
Q_OBJECT
public:
explicit test_union(QObject *parent = nullptr);
signals:
private slots:
void initTestCase();
void testtolower();
void testtolower_data();
};
#endif // TEST_UNION_H
test_union.cpp
#include "test_union.h"
test_union::test_union(QObject *parent) : QObject(parent)
{
}
void test_union::testtolower()
{
// 获取测试数据
QFETCH(QString, string);
QFETCH(QString, result);
// 如果输入的两个参数不同,则会被认为测试失败
QCOMPARE(string.toLower(), result);
}
void test_union::testtolower_data()
{
// 添加数据列
QTest::addColumn<QString>("string");
QTest::addColumn<QString>("result");
// 添加测试数据
QTest::newRow("lower") << "hello" << "hello";
QTest::newRow("mixed") << "HeLlO" << "hello";
QTest::newRow("upper") << "HELLO" << "hello";
////////////////////////////////////////////////////////////
// string result
// lower hello hello
// mixed HeLIO hello
// upper HELLO hello
////////////////////////////////////////////////////////////
}
void test_union::initTestCase()
{
//测试内置的初始化函数
qDebug()<<"call before all";
}
//这条语句是建立测试程序的主函数,相对应的,程序中不能存在其他主函数,可以先将程序中main.cpp的主函数注释掉。
QTEST_MAIN(test_union)
3 运行结果
********* Start testing of test_union *********
Config: Using QtTest library 5.9.1, Qt 5.9.1 (x86_64-little_endian-lp64 shared (dynamic) release build; by GCC 5.3.1 20160406 (Red Hat 5.3.1-6))
QDEBUG : test_union::initTestCase() call before all
PASS : test_union::initTestCase()
PASS : test_union::testtolower(lower)
PASS : test_union::testtolower(mixed)
PASS : test_union::testtolower(upper)
PASS : test_union::cleanupTestCase()
Totals: 5 passed, 0 failed, 0 skipped, 0 blacklisted, 0ms
********* Finished testing of test_union *********
网友评论