I'm trying to use a superclass to define common constructor, initTestCase()
, cleanupTestCase()
, init()
and cleanup()
code for tests.
class BaseTest : public QObject
{
Q_OBJECT
protected slots:
void initTestCase();
void init();
void cleanup();
void cleanupTestCase();
}
class Test1 : public BaseTest
{
protected slots:
void test1();
void test2();
}
QTEST_MAIN(Test1)
Upon execution, the superclass slots (initTestCase()
, cleanupTestCase()
) are executed, but the subclass slots are ignored.
Is there a way to execute subclasses' tests in this context? How should common behavior be organized in QT Tests?
Edit: this has been fixed with the use of private slots, but there seems to be a repetition (nested execution), showing in the Test Results pane, relative to the plain output. Some sample code is posted.
parent.h:
#ifndef PARENT_H
#define PARENT_H
#include <QObject>
#include <QtTest>
class Parent : public QObject
{
Q_OBJECT
public:
private slots:
void initTestCase();
void init();
void cleanup();
void cleanupTestCase();
};
#endif // PARENT_H
parent.cpp:
#include "parent.h"
void Parent::initTestCase() {}
void Parent::cleanupTestCase() {}
void Parent::init() {}
void Parent::cleanup() {}
testwithparent.h:
#ifndef TESTWITHPARENT_H
#define TESTWITHPARENT_H
#include <QObject>
#include <QtTest>
#include "parent.h"
class TestWithParent : public Parent
{
Q_OBJECT
public:
private slots:
void test1();
};
#endif // TESTWITHPARENT_H
testwithparent.cpp:
#include "testwithparent.h"
void TestWithParent::test1() {
Q_ASSERT(true);
}
QTEST_MAIN(TestWithParent)
Console output:
PASS : TestWithParent::initTestCase()
PASS : TestWithParent::test1()
PASS : TestWithParent::cleanupTestCase()
Totals: 3 passed, 0 failed, 0 skipped, 0 blacklisted
Test Results output:
Test summary: 6 passes, 0 fails. <-- double test count
Executing test case TestWithParent
Executing test function initTestCase
Executing test function test1
Executing test function cleanupTestCase
Executing test case TestWithParent <-- repetition