我一直在努力创建一个单元测试。我需要学习这个来测试我们的 gui 对象。到目前为止,这个网站已经回答了很多问题(大多数问题以前被问过和回答过)。但是,我现在遇到了一个错误,我找不到帮助我解决它的链接。我得到“没有规则来制作'TestGui.o'需要的目标'TestGui.cpp'。停止”。谁能告诉我如何解决这个问题并编译我的测试?这是我第一次尝试测试...
这是我的测试文件-
#include <QString>
#include <QObject>
#include <QtTest>
#include <QLineEdit>
#include <QWidget>
#include <QApplication>
class SampleTest : public QObject
{
Q_OBJECT
public:
SampleTest();
private Q_SLOTS:
void initTestCase();
void cleanupTestCase();
void TestGui();
void TestQstring();
};
SampleTest::SampleTest()
{
}
void SampleTest::initTestCase()
{
}
void SampleTest::cleanupTestCase()
{
}
void SampleTest::TestGui()
{
QLineEdit line_edit;
QTest::keyClicks(&line_edit, "hello world");
QCOMPARE(line_edit.text(), QString("hello world"));
}
void SampleTest::TestQstring()
{
QString str = "hello world";
QCOMPARE(str.toUpper(), QString("HELLO wORLD"));
}
QTEST_MAIN(SampleTest)
#include "SampleTest.moc"
我的 test.pro 文件在这里 -
#-------------------------------------------------
#
# Project created by QtCreator 2015-09-03T09:46:40
#
#-------------------------------------------------
QT += core widgets gui testlib
TARGET = SampleTest
CONFIG += console
CONFIG -= app_bundle
TEMPLATE = app
SOURCES += ../sample_project/TestGui.cpp \
SampleTest.cpp
HEADERS += ../sample_project/TestGui.h
FORMS += ../sample_project/TestGui.ui
DEFINES += SRCDIR=\\\"$$PWD/\\\"
INCLUDEPATH += $$PWD/../sample_project
include(../sample_project/sample_project.pri)
这是我的项目 .pro 文件 -
#-------------------------------------------------
#
# Project created by QtCreator 2015-09-03T09:29:42
#
#-------------------------------------------------
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = sample_project
TEMPLATE = app
SOURCES += main.cpp\
TestGui.cpp \
TestBox.cpp
HEADERS += TestGui.h \
TestBox.h
FORMS += TestGui.ui \
TestBox.ui
我的项目 .pri 文件是我项目的源代码和标头 -
SOURCES += TestGui.cpp
HEADRES += TestGui.h