【发布时间】:2017-12-07 18:48:52
【问题描述】:
我一直在努力创建一个单元测试。我需要学习这个来测试我们的 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
【问题讨论】:
-
我认为这意味着找不到TestGui.cpp,即存在路径问题。看来你是从不同的地方跑来跑去的?
-
谢谢。我刚刚修复了路径问题。变化反映在上面。但是,我仍然收到错误消息。我注意到,如果我注释掉我的测试 .pri 文件中的最后一个包含,它会编译并运行。但是,它不会从项目中调出我的窗口。我正在尝试在 lineEdit 中打开窗口并键入“hello world”。
-
好的,我意识到我需要在我的项目 .pri 文件中进行类似的更改。它编译干净,但现在我的问题是如何让它在我的项目中运行 main?我会在网站上搜索答案。如果你有一些想法,我很乐意听到。再次感谢。
标签: qtestlib