FactDev  0.1
testrunner.h
1 /*
2  * Thanks to
3  * https://marcoarena.wordpress.com/2012/06/23/increase-your-qtest-productivity
4  *
5  * @aroquemaurel
6  */
7 
8 #ifndef TESTRUNNER_H
9 #define TESTRUNNER_H
10 
11 // Qt includes
12 #include <QTest>
13 #include <QSharedPointer>
14 // std includes
15 #include <algorithm>
16 #include <list>
17 #include <iostream>
18 
19 // Test Runner allows automatic execution of tests
21 {
22 public:
23  static TestRunner& Instance();
24 
25  template <typename T> char RegisterTest(QString name) {
26  if ( std::find_if( begin(m_tests), end(m_tests), [&name](QSharedPointer<QObject>& elem)
27  { return elem->objectName() == name; }) == end(m_tests) ) {
28  QSharedPointer<QObject> test(new T());
29  test->setObjectName(name);
30  m_tests.push_back(test);
31  }
32  return char(1);
33  }
34 
35  int RunAll();
36 
37 private:
38  std::list<QSharedPointer<QObject>> m_tests;
39 };
40 
41 // Use this macro after your test declaration
42 #define DECLARE_TEST(className)\
43  static char test_##className = TestRunner::Instance().RegisterTest<className>(QString(#className));
44 
45 // Use this macro to execute all tests
46 #define RUN_ALL_TESTS()\
47  TestRunner::Instance().RunAll();
48 
49 #endif // TESTRUNNER_H
Definition: testrunner.h:20