AUnit  0.4.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()->setStatusMatchingPattern(pattern, Test::kStatusSkipped);
58  }
59 
64  static void include(const char* pattern) {
65  getRunner()->setStatusMatchingPattern(pattern, Test::kStatusNew);
66  }
67 
69  static void setVerbosity(uint8_t verbosity) {
70  getRunner()->setVerbosityFlag(verbosity);
71  }
72 
74  static bool isVerbosity(uint8_t verbosity) {
75  return getRunner()->isVerbosityFlag(verbosity);
76  }
77 
79  static void setPrinter(Print* printer);
80 
87  static void setTimeout(TimeoutType seconds) {
88  getRunner()->setRunnerTimeout(seconds);
89  }
90 
91  private:
92  // 10 second timeout for the runner
93  static const TimeoutType kTimeoutDefault = 10;
94 
96  static TestRunner* getRunner();
97 
99  static uint16_t countTests();
100 
101  // Disable copy-constructor and assignment operator
102  TestRunner(const TestRunner&) = delete;
103  TestRunner& operator=(const TestRunner&) = delete;
104 
106  TestRunner();
107 
109  void runTest();
110 
112  void listTests();
113 
115  void printStartRunner();
116 
118  void resolveRun();
119 
121  void setupRunner();
122 
124  void setVerbosityFlag(uint8_t verbosity) { mVerbosity = verbosity; }
125 
127  bool isVerbosityFlag(uint8_t verbosity) { return mVerbosity & verbosity; }
128 
130  void setStatusMatchingPattern(const char* pattern, uint8_t status);
131 
133  void setRunnerTimeout(TimeoutType seconds);
134 
135  // The current test case is represented by a pointer to a pointer. This
136  // allows treating the root node the same as all the other nodes, and
137  // simplifies the code traversing the singly-linked list significantly.
138  Test** mCurrent;
139 
140  bool mIsResolved;
141  bool mIsSetup;
142  bool mIsRunning;
143  uint8_t mVerbosity;
144  uint16_t mCount;
145  uint16_t mPassedCount;
146  uint16_t mFailedCount;
147  uint16_t mSkippedCount;
148  uint16_t mExpiredCount;;
149  TimeoutType mTimeout;
150  unsigned long mStartTime;
151 };
152 
153 }
154 
155 #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:87
static void include(const char *pattern)
Include the tests which match the pattern.
Definition: TestRunner.h:64
static const uint8_t kStatusNew
Test is new, needs to be setup.
Definition: Test.h:55
static void setVerbosity(uint8_t verbosity)
Set the verbosity flag.
Definition: TestRunner.h:69
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 bool isVerbosity(uint8_t verbosity)
Returns true if ANY of the bit flags of &#39;verbosity&#39; is set.
Definition: TestRunner.h:74