我一直致力于创建单元测试。我需要学习这个来测试我们的gui对象。到目前为止,该网站已经回答了很多问题(大多数问题都是先前提出并回答过但是,我现在处于一个错误的位置,我找不到帮助我解决它的链接。我得到“没有规则来制作目标'TestGui.cpp',需要'TestGui.o'。停止”。任何人都可以告诉我如何解决这个并让我的测试编译?这是我第一次尝试测试...
这是我的测试文件 -
#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
答案 0 :(得分:0)
尝试根据文件名重命名类名。在您的情况下,SampleTest =&gt; TestGui
QtCreator尝试找到合适的类定义。如果没有,它会忽略创建目标&#34; .moc:&#34;在项目Makefile中。