lundi 27 avril 2020

QTest with QtCreator, button run all test grayed

I began a project using Qt. I want to add some tests to this project. I want to split tests over differents categories.

  1. Tests for exporting data
  2. Tests for computing data
  3. Tests for manipulating data...

To do this, I created this simple .pro file:

QT += testlib
QT += gui
CONFIG += qt warn_on depend_includepath testcase

TEMPLATE = app

SOURCES +=  \
    export_test/tst_export_all_log.cpp \
    export_test/tst_export_last_log.cpp \
    main.cpp

HEADERS += \
    TestSuite.h \
    export_test/ExportTest.h

Here is the associated main file It is a simple main that run the QTest::qExec function over all tests The categories can be enabled or disabled thanks to a DEFINE variable

#include <QtTest>

#include "export_test/ExportTest.h"

#define EXPORT_TEST 1

int main(int argc, char *argv[]) {
    int status = 0;

    auto runTests = [&](const auto &tests) {
        for (auto test : tests) {
            status |= QTest::qExec(test, argc, argv);
        }
    };

#if EXPORT_TEST
    runTests(ExportTest::suite());
#endif
    return status;
}

Here is the ExportTest class. The idea is to create static object that are automatically registered inside the vector statically initialized within the suite() method.

#pragma once

#include <QObject>

template <typename T>
class TestSuite : public QObject {
  public:
    TestSuite() { suite().push_back(this); }

    static std::vector<QObject *> &suite() {
        static std::vector<QObject *> vector;
        return vector;
    }
};


class ExportTest : public TestSuite<ExportTest> {
    Q_OBJECT
  public:
    using TestSuite<ExportTest>::TestSuite;
};

Here is the "implementation" of one test

#include <QtTest>
#include <QCoreApplication>

#include "ExportTest.h"

class export_last_log : public ExportTest {
    Q_OBJECT

  public:
  private slots:

    void test_case1();
};

void export_last_log::test_case1() { QCOMPARE(1, 2); }

#include "tst_export_last_log.moc"

static export_last_log export_last_log;

Everything seems to work, when I run it, I obtain this :

********* Start testing of export_all_log *********
Config: Using QtTest library 5.9.9, Qt 5.9.9 (x86_64-little_endian-lp64 shared (dynamic) release build; by GCC 5.3.1 20160406 (Red Hat 5.3.1-6))
PASS   : export_all_log::initTestCase()
PASS   : export_all_log::test_case1()
PASS   : export_all_log::cleanupTestCase()
Totals: 3 passed, 0 failed, 0 skipped, 0 blacklisted, 0ms
********* Finished testing of export_all_log *********
********* Start testing of export_last_log *********
Config: Using QtTest library 5.9.9, Qt 5.9.9 (x86_64-little_endian-lp64 shared (dynamic) release build; by GCC 5.3.1 20160406 (Red Hat 5.3.1-6))
PASS   : export_last_log::initTestCase()
FAIL!  : export_last_log::test_case1() Compared values are not the same
   Actual   (1): 1
   Expected (2): 2
   Loc: [../../src/core_tests/export_test/tst_export_last_log.cpp(15)]
PASS   : export_last_log::cleanupTestCase()
Totals: 2 passed, 1 failed, 0 skipped, 0 blacklisted, 0ms
********* Finished testing of export_last_log *********

However, Impossible to launch tests from the "8. Test Results" tab of QtCreator. The buttons are all grayed Impossible to see which test have failed easily, the view is empty.

Maybe I have not well understood how can we make testing with Qt? Or maybe I forgot to do something in the .pro file to force QtCreator to recognize it as tests?

Aucun commentaire:

Enregistrer un commentaire