2

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
user182917
  • 1,688
  • 2
  • 14
  • 17
  • 3
    Use `private slots`, not `protected slots`. – RA. Jul 05 '19 at 16:34
  • This solved the parent execution problem. However, in this configuration the test subclass runs more than one time. I get output like: ``` Executing test case Test1 Executing test function initTestCase Executing test function test1 Executing test function cleanupTestCase Executing test case Test1 Executing test function initTestCase Executing test function test1 Executing test function cleanupTestCase ``` – user182917 Jul 08 '19 at 17:05
  • Post more of your code and the output you're getting. I can't reproduce the behavior you're seeing. – RA. Jul 08 '19 at 17:59
  • Code has been posted. – user182917 Jul 08 '19 at 19:00
  • Actually, the repeating behavior is independent of whether the test is subclassed or not. It is in the Test Results pane relative to the plain output. – user182917 Jul 08 '19 at 19:14

0 Answers0