Skip to content

Instantly share code, notes, and snippets.

@malcom malcom/QTestSuite.h
Last active Dec 26, 2015

Embed
What would you like to do?
QTestSuite - Simple tool for organising your test cases and test suites into hierarchy.
#include <QtTest/QtTest>
#include "QTestSuite.h"
class TestCase1 : public QObject {
Q_OBJECT
private slots:
void test1() {
QVERIFY(1 + 2 == 2);
}
void test2() {
QCOMPARE(1 + 1, 2);
}
};
class TestCase2 : public QObject {
Q_OBJECT
private slots:
void test1() {
QVERIFY(2 + 2 > 2);
}
void test2() {
QCOMPARE(2, 2);
}
};
QTEST_SUITE_REGISTRATION(TestCase1);
QTEST_SUITE_REGISTRATION(TestCase2);
QTEST_SUITE_MAIN();
#include "main.moc"
#include <QtTest/QtTest>
#include "QTestSuite.h"
class TestCase1 : public QObject {
Q_OBJECT
private slots:
void test1() {
QVERIFY(1 + 2 == 2);
}
void test2() {
QCOMPARE(1 + 1, 2);
}
};
class TestCase2 : public QObject {
Q_OBJECT
private slots:
void test1() {
QVERIFY(2 + 2 > 2);
}
void test2() {
QCOMPARE(2, 2);
}
};
int main(int argc, char* argv[]) {
TestSuite suite("myTestSuite");
suite.add(new TestCase1());
suite.add(new TestCase2());
TestRunner::master()->add(&suite);
return TestRunner::run(argc, argv);
}
#include "main.moc"
// QTestSuite
// Simple tool for organising your test cases and test suites into hierarchy.
// Marcin 'Malcom' Malich <me@malcom.pl>
// http://blog.malcom.pl/2013/10/20/qtestsuite/
// MIT License
#ifndef QTEST_SUITE_H
#define QTEST_SUITE_H
#include <QTest>
#include <QVector>
class TestSuite : public QObject {
Q_OBJECT
public:
TestSuite(const QString& name = "")
: m_name(name) {}
~TestSuite() {}
void add(QObject* test) {
m_test.push_back(test);
}
void remove(QObject* test) {
int id = m_test.indexOf(test);
if (id != -1)
m_test.remove(id);
}
const QString& getName() const {
return m_name;
}
const QVector<QObject*>& getTests() const {
return m_test;
}
void countTests() const {
m_test.size();
}
private:
QString m_name;
QVector<QObject*> m_test;
};
class TestRunner {
public:
static TestSuite* master() {
static TestSuite master;
return &master;
}
static int run(int argc, char* argv[]) {
return run(master(), argc, argv);
}
static int run(QObject* test, int argc, char* argv[]) {
return do_run(test, argc, argv);
}
private:
static int do_run(QObject* obj, int argc, char* argv[]) {
int ret = 0;
TestSuite* s = qobject_cast<TestSuite*>(obj);
if (s != NULL) {
foreach (QObject* obj, s->getTests())
ret |= do_run(obj, argc, argv);
} else {
ret |= QTest::qExec(obj, argc, argv);
}
return ret;
}
};
template<typename TestObject>
struct AutoRegisterSuite {
AutoRegisterSuite() {
TestRunner::master()->add(&test);
}
~AutoRegisterSuite() {
TestRunner::master()->remove(&test);
}
TestObject test;
};
#define QTEST_SUITE_REGISTRATION(TestObject) \
static AutoRegisterSuite<TestObject> autoRegister##TestObject;
#define QTEST_SUITE_APPLESS_MAIN() \
int main(int argc, char* argv[]) { \
return TestRunner::run(argc, argv); \
}
#define QTEST_SUITE_MAIN() \
int main(int argc, char* argv[]) { \
QCoreApplication app(argc, argv); \
return TestRunner::run(argc, argv); \
}
#endif // QTEST_SUITE_H
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.