2 * Copyright (c) 2011-2015 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 * @author Przemyslaw Dobrowolski (p.dobrowolsk@samsung.com)
19 * @author Lukasz Wrzosek (l.wrzosek@samsung.com)
21 * @brief This file is the header file of test runner
24 #ifndef DPL_TEST_RUNNER_H
25 #define DPL_TEST_RUNNER_H
40 #include <dpl/atomic.h>
41 #include <dpl/availability.h>
42 #include <dpl/colors.h>
43 #include <dpl/gdbbacktrace.h>
44 #include <dpl/singleton.h>
45 #include <dpl/test/performance_result.h>
46 #include <dpl/test/test_exception.h>
47 #include <dpl/test/test_failed.h>
48 #include <dpl/test/test_ignored.h>
49 #include <dpl/test/test_result.h>
50 #include <dpl/test/test_results_collector.h>
56 typedef std::map<std::string, TestResultsCollectorBasePtr>
57 TestResultsCollectors;
58 TestResultsCollectors m_collectors;
60 std::string m_startTestId;
63 std::queue<std::string> m_failReason;
67 m_currentTestCase(nullptr)
69 , m_allowChildLogs(false)
71 , m_firstDeferredExceptionType(DeferredExceptionType::DEFERRED_FAILED)
74 void beginPerformance(std::chrono::system_clock::duration maxDurationInMicroseconds);
75 void endPerformance();
76 void setCurrentTestCasePerformanceResult(const PerformanceResultPtr &performance);
77 ConstPerformanceResultPtr getCurrentTestCasePerformanceResult();
79 void addFailReason(const std::string &reason);
81 typedef void (*TestCase)();
88 PerformanceResultPtr performance;
90 bool operator <(const TestCaseStruct &other) const
92 return name < other.name;
95 bool operator ==(const TestCaseStruct &other) const
97 return name == other.name;
100 TestCaseStruct(const std::string &n, TestCase p) :
106 typedef std::list<TestCaseStruct> TestCaseStructList;
107 typedef std::map<std::string, TestCaseStructList> TestCaseGroupMap;
108 TestCaseGroupMap m_testGroups;
110 TestCaseStruct * m_currentTestCase;
112 std::string m_currentGroup;
114 // Terminate without any logs.
115 // Some test requires to call fork function.
116 // Child process must not produce any logs and should die quietly.
118 bool m_allowChildLogs;
121 void InvalidArgs(const std::string& message = "Invalid arguments!");
124 bool filterGroupsByXmls(const std::vector<std::string> & files);
125 bool filterByXML(std::map<std::string, bool> & casesMap);
126 void normalizeXMLTag(std::string& str, const std::string& testcase);
128 void RunTestCase(const TestCaseStruct& testCase);
130 void setCurrentTestCase(TestCaseStruct* testCase);
131 TestCaseStruct *getCurrentTestCase();
135 std::string getConcatedFailReason(const std::string &reason);
137 void CollectResult(const std::string& id, const TestResult &result);
140 void RegisterTest(const char *testName, TestCase proc);
141 void InitGroup(const char* name);
143 int ExecTestRunner(int argc, char *argv[]);
144 typedef std::vector<std::string> ArgsList;
145 int ExecTestRunner(ArgsList args);
146 // The runner will terminate as soon as possible (after current test).
148 bool GetAllowChildLogs();
150 void deferFailedException(const DPL::Test::TestFailed &ex);
151 void deferIgnoredException(const DPL::Test::TestIgnored &ex);
156 std::vector<std::string> m_deferredExceptionsMessages;
157 std::size_t m_deferDeepness;
158 enum DeferredExceptionType {
161 } m_firstDeferredExceptionType;
162 DPL::Test::TestFailed m_firstDeferredFail;
163 DPL::Test::TestIgnored m_firstDeferredIgnore;
166 typedef DPL::Singleton<TestRunner> TestRunnerSingleton;
170 #define RUNNER_TEST_GROUP_INIT(GroupName) \
171 static int Static##GroupName##Init() \
173 DPL::Test::TestRunnerSingleton::Instance().InitGroup(#GroupName); \
176 const int DPL_UNUSED Static##GroupName##InitVar = \
177 Static##GroupName##Init();
179 #define RUNNER_TEST(Proc) \
181 static int Static##Proc##Init() \
183 DPL::Test::TestRunnerSingleton::Instance().RegisterTest(#Proc, &Proc); \
186 const int DPL_UNUSED Static##Proc##InitVar = Static##Proc##Init(); \
193 * Use them to create assertions in test cases. To do that put them inside test
194 * body. Failing assertion indicates failing test.
197 #define RUNNER_ASSERT_MSG(test, message) \
202 std::ostringstream assertMsg; \
203 assertMsg << message << DPL::gdbbacktrace(); \
204 DPL::Test::TestFailed e(#test, \
208 if (!std::uncaught_exception()) \
210 DPL::Test::TestRunnerSingleton::Instance().addFailReason(e.GetMessage()); \
214 #define RUNNER_ASSERT_ERRNO_MSG(test, message) \
219 const char *err = strerror(errno); \
220 std::ostringstream assertMsg; \
221 assertMsg << message; \
222 if (!assertMsg.str().empty()) \
224 assertMsg << err << DPL::gdbbacktrace(); \
225 DPL::Test::TestFailed e(#test, \
229 if (!std::uncaught_exception()) \
231 DPL::Test::TestRunnerSingleton::Instance().addFailReason(e.GetMessage()); \
235 #define RUNNER_ASSERT_ERRNO(test) \
236 RUNNER_ASSERT_ERRNO_MSG(test, "")
238 #define RUNNER_FAIL_MSG(message) \
239 RUNNER_ASSERT_MSG(false, message)
241 #define RUNNER_ASSERT(test) \
242 RUNNER_ASSERT_MSG(test, "")
247 * When test reaches this macro call, its furhter code will be ignored.
248 * To ignore whole test, put this macro call at the beginning of this tests
252 #define RUNNER_IGNORED_MSG(message) \
255 std::ostringstream assertMsg; \
256 assertMsg << message; \
257 throw DPL::Test::TestIgnored(assertMsg.str()); \
263 * Use these macros to do the time measurement. The first macro will start time measurement,
264 * the second will gather the result. These macros can be used only once per test-case.
265 * The result of time measurement will be displayed only if the test will pass.
266 * Notice that these macros will work only if will be used in parent process. If these
267 * macros will be used in child process then there will be no time measure results printed.
268 * This macro in multiprocess tests has effect only if used in parent process. This macro
269 * used in child process in multiprocess test has no effect.
270 * The precision of measurement is 1 microsecond - the smallest time value that can be
271 * measured is 0.000001s.
272 * The time measure results will be printed only specific output format:
278 #define RUNNER_PERF_TEST_BEGIN(maxTime) \
280 DPL::Test::TestRunnerSingleton::Instance().beginPerformance( \
281 std::chrono::microseconds{static_cast<long long int>(maxTime*1000000.0)}); \
284 #define RUNNER_PERF_TEST_END() \
286 DPL::Test::TestRunnerSingleton::Instance().endPerformance(); \
292 * Use these macros to print error messages during test run time
295 #define RUNNER_ERROR_MSG(message) \
297 std::cerr << DPL::Colors::Text::RED_BEGIN << message \
298 << DPL::Colors::Text::RED_END << std::endl; \
304 * Use them to defer fails and ignores in test cases.
305 * Some code constructions disallow to throw. Such places can be surrounded
306 * with RUNNER_DEFER_SCOPE macro. RUNNER_DEFER_TRYCATCH macro can be used to catch possibly thrown
307 * exceptions within such scope. Possibly catched exceptions will be rethrown
308 * when leaving RUNNER_DEFER_SCOPE scope.
309 * Macros can be safely nested.
313 #define RUNNER_DEFER_TRYCATCH(scope) \
319 catch (const DPL::Test::TestFailed &ex) \
321 DPL::Test::TestRunnerSingleton::Instance().deferFailedException(ex); \
323 catch (const DPL::Test::TestIgnored &ex) \
325 DPL::Test::TestRunnerSingleton::Instance().deferIgnoredException(ex); \
329 #define RUNNER_DEFER_SCOPE(scope) \
331 DPL::Test::TestRunnerSingleton::Instance().deferBegin(); \
333 DPL::Test::TestRunnerSingleton::Instance().deferEnd(); \
336 #endif // DPL_TEST_RUNNER_H