我正在尝试使用超类来定义通用构造函数 ,和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 测试中应该如何组织常见行为?
编辑:这已通过使用私有插槽得到修复,但似乎存在重复(嵌套执行),显示在测试结果窗格中,相对于普通输出。发布了一些示例代码。
父.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
父级.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