I have setup a TestSuite in Qt5 basically following the instructions from here and here. It works as described; however; when I switch from the Projects view to Tests view it does NOT show the individual class tests as shown in the TestsView image below. I expect it to show my test class and the individual function slots. This is useful for when I am debugging or I would only like to execute a single test function in the test class.
Workaround solution I found: In main.cpp, if I instantiate the class and call qExec() instead of using a QOject* to the test class instance then it works (show in main.cpp below); however, this defeats the purpose of the test suite class.
// testsuite.h
#pragma once
// Qt headers
#include <QObject>
#include <QtTest/QtTest>
class TestSuite : public QObject
{
Q_OBJECT
public:
explicit TestSuite();
virtual ~TestSuite();
static QVector<QObject*>& suite();
};
// testsuite.cpp
#include "testsuite.h"
#include <QDebug>
TestSuite::TestSuite()
{
suite().push_back(this);
}
TestSuite::~TestSuite() {}
QVector<QObject*>& TestSuite::suite()
{
static QVector<QObject*> instance;
return instance;
}
// main.cpp
#include "testsuite.h"
#include <QtTest>
int main(int argc, char* argv[])
{
Q_UNUSED(argc)
Q_UNUSED(argv)
int failedTestsCount = 0;
for (auto &test : TestSuite::suite()) {
int result = QTest::qExec(test);
if (result != 0) {
failedTestsCount++;
}
}
// Work around w/ #include class file
//TestExampleClass testExampleClass ;
//QTest::qExec(&testExampleClass );
return failedTestsCount;
}
// testexampleclass.h
#include <QtTest/QtTest>
#include "testsuite.h"
class TestExampleClass : public TestSuite
{
Q_OBJECT
private slots:
void test_addSomeStuff();
};
// testexampleclass.cpp
#include "testexampleclass.h"
static TestExampleClass sInstance;
// test adding list of numbers
void TestExampleClass::test_addSomeStuff()
{
QVERIFY( true );
}
Edit: I am using Qt Creator 4.1.2 and Qt 5.13.2 (MSVC 2017)
I had the same problem. During a 'Rescan Sests' the QC searches for following entries: {"QTEST_MAIN", "QTEST_APPLESS_MAIN", "QTEST_GUILESS_MAIN"} The following dummy macro code helped:
Than you have o place a
QTEST_MAIN(TestClassName)
into your test class declaration. Afterwards my tests were visible in the Treeview: Qt Creator TestsHowever QTEST_MAIN wont work anymore.