AUnit  0.4.1
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()->setStatusMatchingPattern(pattern, Test::kStatusSkipped);
58  }
59 
66  static void exclude(const char* testClass, const char* pattern) {
67  getRunner()->setStatusMatchingPattern(testClass, pattern,
69  }
70 
75  static void include(const char* pattern) {
76  getRunner()->setStatusMatchingPattern(pattern, Test::kStatusNew);
77  }
78 
85  static void include(const char* testClass, const char* pattern) {
86  getRunner()->setStatusMatchingPattern(testClass, pattern,
88  }
89 
91  static void setVerbosity(uint8_t verbosity) {
92  getRunner()->setVerbosityFlag(verbosity);
93  }
94 
96  static bool isVerbosity(uint8_t verbosity) {
97  return getRunner()->isVerbosityFlag(verbosity);
98  }
99 
101  static void setPrinter(Print* printer);
102 
109  static void setTimeout(TimeoutType seconds) {
110  getRunner()->setRunnerTimeout(seconds);
111  }
112 
113  private:
114  // 10 second timeout for the runner
115  static const TimeoutType kTimeoutDefault = 10;
116 
118  static TestRunner* getRunner();
119 
121  static uint16_t countTests();
122 
123  // Disable copy-constructor and assignment operator
124  TestRunner(const TestRunner&) = delete;
125  TestRunner& operator=(const TestRunner&) = delete;
126 
128  TestRunner();
129 
131  void runTest();
132 
134  void listTests();
135 
137  void printStartRunner();
138 
140  void resolveRun();
141 
143  void setupRunner();
144 
146  void setVerbosityFlag(uint8_t verbosity) { mVerbosity = verbosity; }
147 
149  bool isVerbosityFlag(uint8_t verbosity) { return mVerbosity & verbosity; }
150 
152  void setStatusMatchingPattern(const char* pattern, uint8_t status);
153 
158  void setStatusMatchingPattern(const char* testClass, const char* pattern,
159  uint8_t status);
160 
162  void setRunnerTimeout(TimeoutType seconds);
163 
164  // The current test case is represented by a pointer to a pointer. This
165  // allows treating the root node the same as all the other nodes, and
166  // simplifies the code traversing the singly-linked list significantly.
167  Test** mCurrent;
168 
169  bool mIsResolved;
170  bool mIsSetup;
171  bool mIsRunning;
172  uint8_t mVerbosity;
173  uint16_t mCount;
174  uint16_t mPassedCount;
175  uint16_t mFailedCount;
176  uint16_t mSkippedCount;
177  uint16_t mExpiredCount;;
178  TimeoutType mTimeout;
179  unsigned long mStartTime;
180 };
181 
182 }
183 
184 #endif
The class that runs the various test cases defined by the test() and testing() macros.
Definition: TestRunner.h:41
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:109
static void include(const char *pattern)
Include the tests which match the pattern.
Definition: TestRunner.h:75
static const uint8_t kStatusNew
Test is new, needs to be setup.
Definition: Test.h:55
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:66
static void setVerbosity(uint8_t verbosity)
Set the verbosity flag.
Definition: TestRunner.h:91
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 kStatusSkipped
Test is skipped, through the exclude() method or skip() was called.
Definition: Test.h:67
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 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:85
static bool isVerbosity(uint8_t verbosity)
Returns true if ANY of the bit flags of &#39;verbosity&#39; is set.
Definition: TestRunner.h:96