AUnit  1.0.0
Unit testing framework for Arduino platforms inspired by ArduinoUnit and Google Test.
TestRunner.h
1 /*
2 MIT License
3 
4 Copyright (c) 2018 Brian T. Park
5 
6 Permission is hereby granted, free of charge, to any person obtaining a copy
7 of this software and associated documentation files (the "Software"), to deal
8 in the Software without restriction, including without limitation the rights
9 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 copies of the Software, and to permit persons to whom the Software is
11 furnished to do so, subject to the following conditions:
12 
13 The above copyright notice and this permission notice shall be included in all
14 copies or substantial portions of the Software.
15 
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 SOFTWARE.
23 */
24 
25 #ifndef AUNIT_TEST_RUNNER_H
26 #define AUNIT_TEST_RUNNER_H
27 
28 #include <stdint.h>
29 #include "Test.h"
30 
31 class Print;
32 
33 namespace aunit {
34 
41 class TestRunner {
42  public:
44  typedef uint8_t TimeoutType;
45 
47  static void run() { getRunner()->runTest(); }
48 
50  static void list() { getRunner()->listTests(); }
51 
56  static void exclude(const char* pattern) {
57  getRunner()->setLifeCycleMatchingPattern(
58  pattern, Test::kLifeCycleExcluded);
59  }
60 
67  static void exclude(const char* testClass, const char* pattern) {
68  getRunner()->setLifeCycleMatchingPattern(testClass, pattern,
70  }
71 
76  static void include(const char* pattern) {
77  getRunner()->setLifeCycleMatchingPattern(pattern, Test::kLifeCycleNew);
78  }
79 
86  static void include(const char* testClass, const char* pattern) {
87  getRunner()->setLifeCycleMatchingPattern(testClass, pattern,
89  }
90 
92  static void setVerbosity(uint8_t verbosity) {
93  getRunner()->setVerbosityFlag(verbosity);
94  }
95 
97  static bool isVerbosity(uint8_t verbosity) {
98  return getRunner()->isVerbosityFlag(verbosity);
99  }
100 
102  static void setPrinter(Print* printer);
103 
110  static void setTimeout(TimeoutType seconds) {
111  getRunner()->setRunnerTimeout(seconds);
112  }
113 
114  private:
115  // 10 second timeout for the runner
116  static const TimeoutType kTimeoutDefault = 10;
117 
119  static TestRunner* getRunner();
120 
122  static uint16_t countTests();
123 
124  // Disable copy-constructor and assignment operator
125  TestRunner(const TestRunner&) = delete;
126  TestRunner& operator=(const TestRunner&) = delete;
127 
129  TestRunner();
130 
132  void runTest();
133 
135  void listTests();
136 
138  void printStartRunner();
139 
141  void resolveRun();
142 
144  void setupRunner();
145 
147  void setVerbosityFlag(uint8_t verbosity) { mVerbosity = verbosity; }
148 
150  bool isVerbosityFlag(uint8_t verbosity) { return mVerbosity & verbosity; }
151 
153  void setLifeCycleMatchingPattern(const char* pattern, uint8_t lifeCycle);
154 
159  void setLifeCycleMatchingPattern(const char* testClass, const char* pattern,
160  uint8_t lifeCycle);
161 
163  void setRunnerTimeout(TimeoutType seconds);
164 
165  // The current test case is represented by a pointer to a pointer. This
166  // allows treating the root node the same as all the other nodes, and
167  // simplifies the code traversing the singly-linked list significantly.
168  Test** mCurrent;
169 
170  bool mIsResolved;
171  bool mIsSetup;
172  bool mIsRunning;
173  uint8_t mVerbosity;
174  uint16_t mCount;
175  uint16_t mPassedCount;
176  uint16_t mFailedCount;
177  uint16_t mSkippedCount;
178  uint16_t mExpiredCount;
179  uint16_t mStatusErrorCount;
180  TimeoutType mTimeout;
181  unsigned long mStartTime;
182  unsigned long mEndTime;
183 };
184 
185 }
186 
187 #endif
The class that runs the various test cases defined by the test() and testing() macros.
Definition: TestRunner.h:41
Base class of all test cases.
Definition: Test.h:43
static void exclude(const char *pattern)
Exclude the tests which match the pattern.
Definition: TestRunner.h:56
static void setTimeout(TimeoutType seconds)
Set test runner timeout across all tests, in seconds.
Definition: TestRunner.h:110
static void include(const char *pattern)
Include the tests which match the pattern.
Definition: TestRunner.h:76
static void exclude(const char *testClass, const char *pattern)
Exclude the tests which match the pattern given by (testClass + "_" + pattern), the same concatenatio...
Definition: TestRunner.h:67
static void setVerbosity(uint8_t verbosity)
Set the verbosity flag.
Definition: TestRunner.h:92
uint8_t TimeoutType
Integer type of the timeout parameter.
Definition: TestRunner.h:44
static void run()
Run all tests using the current runner.
Definition: TestRunner.h:47
static const uint8_t kLifeCycleExcluded
Test is Excluded by an exclude() method.
Definition: Test.h:65
static void list()
Print out the known tests.
Definition: TestRunner.h:50
static void setPrinter(Print *printer)
Set the output printer.
Definition: TestRunner.cpp:46
static const uint8_t kLifeCycleNew
Test is new, needs to be setup.
Definition: Test.h:57
static void include(const char *testClass, const char *pattern)
Include the tests which match the pattern given by (testClass + "_" + pattern), the same concatenatio...
Definition: TestRunner.h:86
static bool isVerbosity(uint8_t verbosity)
Returns true if ANY of the bit flags of &#39;verbosity&#39; is set.
Definition: TestRunner.h:97