Я пытаюсь использовать суперкласс для определения общего конструктора, initTestCase()
, cleanupTestCase()
, init()
и cleanup()
код для тестов.
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)
При выполнении слоты суперкласса (initTestCase()
, cleanupTestCase()
) выполняются, но слоты подкласса игнорируются.
Есть ли способ выполнить тесты подклассов в этом контексте? Как организовать общее поведение в QT-тестах?
Редактировать: это было исправлено с использованием частных слотов, но, кажется, есть повторение (вложенное выполнение), отображаемое на панели результатов теста относительно простого вывода. Пример кода опубликован.
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)
Вывод на консоль:
PASS : TestWithParent::initTestCase()
PASS : TestWithParent::test1()
PASS : TestWithParent::cleanupTestCase()
Totals: 3 passed, 0 failed, 0 skipped, 0 blacklisted
Вывод результатов теста:
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