关于使用 Qt Test 的四个特殊插槽的真实世界代码示例
Real world code examples about usage of four special slots of Qt Test
我正在研究 Qt 测试,了解到有四个特殊的插槽在测试用例前后执行。
- initTestCase() will be called before the first testfunction is executed.
- cleanupTestCase() will be called after the last testfunction was executed.
- init() will be called before each testfunction is executed.
- cleanup() will be called after every testfunction.
我知道如何使用它们,因为 Qt 文档中有一个清晰的示例。
但我的问题是我想查看这些插槽的真实世界用法,而不是打印 qDebug 示例。在这些插槽中可以更改什么?
我找到了我自己的问题的答案 here。
Try to always do the initTestCase() and cleanupTestCase() methods, so
they can be executed at the startup and at the end of the test. Those
methods usually are to create and delete objects, as you can see in
the example. If you don't create them, they will be “executed” anyway
but will do nothing.
#ifndef CIRCLETEST_H
#define CIRCLETEST_H
#include <QObject>
#include <QtTest/QtTest>
#include "../Geometry/circle.h"
class CircleTest : public QObject
{
Q_OBJECT
public:
explicit CircleTest(QObject *parent = 0);
private:
Circle *c1;
Circle *c2;
private slots:
void initTestCase();
void testRadius();
void testArea();
void cleanupTestCase();
};
#endif // CIRCLETEST_H
circle.h
#include "circletest.h"
CircleTest::CircleTest(QObject *parent) :
QObject(parent)
{
}
void CircleTest::initTestCase()
{
c1 = new Circle();
c2 = new Circle(10);
}
void CircleTest::testArea()
{
QCOMPARE(c1->getArea(), 3.14);
QCOMPARE(c2->getArea(), 314.0);
}
void CircleTest::testRadius()
{
QCOMPARE(c1->getRadius(), 1.0);
QCOMPARE(c2->getRadius(), 10.0);
}
void CircleTest::cleanupTestCase()
{
delete c1;
delete c2;
}
我正在研究 Qt 测试,了解到有四个特殊的插槽在测试用例前后执行。
- initTestCase() will be called before the first testfunction is executed.
- cleanupTestCase() will be called after the last testfunction was executed.
- init() will be called before each testfunction is executed.
- cleanup() will be called after every testfunction.
我知道如何使用它们,因为 Qt 文档中有一个清晰的示例。
但我的问题是我想查看这些插槽的真实世界用法,而不是打印 qDebug 示例。在这些插槽中可以更改什么?
我找到了我自己的问题的答案 here。
Try to always do the initTestCase() and cleanupTestCase() methods, so they can be executed at the startup and at the end of the test. Those methods usually are to create and delete objects, as you can see in the example. If you don't create them, they will be “executed” anyway but will do nothing.
#ifndef CIRCLETEST_H
#define CIRCLETEST_H
#include <QObject>
#include <QtTest/QtTest>
#include "../Geometry/circle.h"
class CircleTest : public QObject
{
Q_OBJECT
public:
explicit CircleTest(QObject *parent = 0);
private:
Circle *c1;
Circle *c2;
private slots:
void initTestCase();
void testRadius();
void testArea();
void cleanupTestCase();
};
#endif // CIRCLETEST_H
circle.h
#include "circletest.h"
CircleTest::CircleTest(QObject *parent) :
QObject(parent)
{
}
void CircleTest::initTestCase()
{
c1 = new Circle();
c2 = new Circle(10);
}
void CircleTest::testArea()
{
QCOMPARE(c1->getArea(), 3.14);
QCOMPARE(c2->getArea(), 314.0);
}
void CircleTest::testRadius()
{
QCOMPARE(c1->getRadius(), 1.0);
QCOMPARE(c2->getRadius(), 10.0);
}
void CircleTest::cleanupTestCase()
{
delete c1;
delete c2;
}