Qt在VS中使用单元测试

在工程的解决方案中,新建一个单元测试项目AutoTest,Qt控制台项目,勾选Core,Test模块

创建CTest类

#include <QObject>
#include <QtTest>

class CTest : public QObject
{
	Q_OBJECT

public:
	CTest();

	~CTest();

private slots:

	//在第一个用例被调用之前,该函数执行,做一些初始化工作
	void initTestCase();

	//最后一个用例执行之后,该函数执行,做一些收尾/反初始化工作
	void cleanupTestCase();

	//每一个用例执行之前,该函数会执行
	void init();

	//每一个用例执行之后,该函数会执行
	void cleanup();

	//测试用例1
	void test_case1();

};


#include "CTest.h"
#include <CFileManager.h>

CTest::CTest()
{

}

CTest::~CTest()
{

}

void CTest::initTestCase()
{

}

void CTest::cleanupTestCase()
{
}

void CTest::init()
{
}

void CTest::cleanup()
{
}

void CTest::test_case1()
{
	CFileManager fileManager;

	//去后缀名
	QVERIFY(fileManager.getSuffixName("RemotePlayback.sln") == ".sln");
}


#include <QtCore/QCoreApplication>
#include "CTest.h"
#include <QDebug>

int main(int argc, char *argv[])
{

	QCoreApplication app(argc, argv); 
	app.setAttribute(Qt::AA_Use96Dpi, true); 

	CTest test;
	QTEST_SET_MAIN_SOURCE_PATH

	qDebug() << QTest::qExec(&test, argc, argv);   //执行test类中的测试用例  全部成功返回0  有失败返回非0

	return app.exec();
}

 

CFileManager为项目中的类

AutoTest的项目属性中的附加包含目录新增 ../QthreadDemo               QThreadDemo为自己的项目目录

CFileManager.h和CFileManager.cpp 通过添加现有项,添加到AutoTest项目中

 

GUI测试

下面提供鼠标操作相关的枚举类型:

ConstantValueDescription
QTest::MousePress0A mouse button is pressed.
QTest::MouseRelease1A mouse button is released.
QTest::MouseClick2A mouse button is clicked (pressed and released).
QTest::MouseDClick3A mouse button is double clicked (pressed and released twice).
QTest::MouseMove4The mouse pointer has moved.
void mouseClick(QWidget *widget, Qt::MouseButton button, Qt::KeyboardModifiers modifier = Qt::KeyboardModifiers(), QPoint pos = QPoint(), int delay = -1)
void mouseClick(QWindow *window, Qt::MouseButton button, Qt::KeyboardModifiers stateKey = Qt::KeyboardModifiers(), QPoint pos = QPoint(), int delay = -1)
void mouseDClick(QWidget *widget, Qt::MouseButton button, Qt::KeyboardModifiers modifier = Qt::KeyboardModifiers(), QPoint pos = QPoint(), int delay = -1)
void mouseDClick(QWindow *window, Qt::MouseButton button, Qt::KeyboardModifiers stateKey = Qt::KeyboardModifiers(), QPoint pos = QPoint(), int delay = -1)
void mouseMove(QWidget *widget, QPoint pos = QPoint(), int delay = -1)
void mouseMove(QWindow *window, QPoint pos = QPoint(), int delay = -1)
void mousePress(QWidget *widget, Qt::MouseButton button, Qt::KeyboardModifiers modifier = Qt::KeyboardModifiers(), QPoint pos = QPoint(), int delay = -1)
void mousePress(QWindow *window, Qt::MouseButton button, Qt::KeyboardModifiers stateKey = Qt::KeyboardModifiers(), QPoint pos = QPoint(), int delay = -1)
void mouseRelease(QWidget *widget, Qt::MouseButton button, Qt::KeyboardModifiers modifier = Qt::KeyboardModifiers(), QPoint pos = QPoint(), int delay = -1)
void mouseRelease(QWindow *window, Qt::MouseButton button, Qt::KeyboardModifiers stateKey = Qt::KeyboardModifiers(), QPoint pos = QPoint(), int delay = -1)

 

©️2020 CSDN 皮肤主题: 编程工作室 设计师:CSDN官方博客 返回首页