Search code examples
c++qtunit-testingqtreewidgetitem

qt unit test QTreeWidgetItem has not been declared


I have one very bad-coded gui app with class Tools(). And i want testing other class Tree() in same file with unit tests.

I had write simple empty Tree class to trying unit tests.

TOOLS.H

#ifndef TOOLS_H
#define TOOLS_H

#include "json.h"
#include <QString>
#include <QList>
#include <QSettings>
...
class Tools
{
public:
    Tools();
    static void createTree(Json::Value parsedJSON, treeElement *Element);
    static void reloadTree(QTreeWidgetItem *item, treeElement *Element);
    ....
    static QMap<QString, QString> Dictionary;
};

class Tree
{
public:
    Tree();

};

#endif // TOOLS_H

Most of code in this file automatically generated by QT. I wrote only isCreatable().

When the test project started, this erorrs has appeared. I don't know why it's not compiling, but i guess some definition and includes error.

TREE_TEST.cpp

#include <QString>
#include <QtTest>
#include "tools.h"

class TreeTest : public QObject
{
    Q_OBJECT

public:
    TreeTest();

private Q_SLOTS:
    void isCreatable();
};

TreeTest::TreeTest()
{
}

void TreeTest::isCreatable()
{
    Tree *newTree = NULL;
    QVERIFY2(newTree == NULL, "Can't create empty Tree");
    newTree = new Tree();
    QVERIFY2(newTree != NULL, "Can't create new instance of Tree");
}

QTEST_APPLESS_MAIN(TreeTest)

#include "tst_treetest.moc"

It's my test pro file. I include path of tested project, and of dependencies (just simple json parsing library).

TREE_TEST.pro

#-------------------------------------------------
#
# Project created by QtCreator 2015-09-06T20:42:18
#
#-------------------------------------------------

QT       += testlib gui

TARGET = tst_treetest
CONFIG   += console
CONFIG   -= app_bundle

TEMPLATE = app

INCLUDEPATH += ../../dependencies/json
INCLUDEPATH += ../../


SOURCES += tst_treetest.cpp
DEFINES += SRCDIR=\\\"$$PWD/\\\"

Please, help me to recognize and solve this trouble. I do not understand, what goes wrong...

P.S. My english is not very good. Please be tolerant.


Solution

  • The goal is add to TREE_TEST.cpp #include <QTreeWidgetItem>. And then add to TREE_TEST.pro QT += widgets

    I found answer there and with help of @Chernobyl

    P.S. If you want start your tests correctly, you need append all headers and sources of tested project in you TEST.pro file. It's needed coz of moc_* files. I tried to found this solution for a long time.