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)
20 * @author Marcin Niesluchowski (m.niesluchow@samsung.com)
22 * @brief This file is the header file of test runner
25 #ifndef DPL_TEST_RUNNER_H
26 #define DPL_TEST_RUNNER_H
42 #include <dpl/atomic.h>
43 #include <dpl/availability.h>
44 #include <dpl/colors.h>
45 #include <dpl/gdbbacktrace.h>
46 #include <dpl/singleton.h>
47 #include <dpl/test/performance_result.h>
48 #include <dpl/test/test_case.h>
49 #include <dpl/test/test_case_extended.h>
50 #include <dpl/test/test_exception.h>
51 #include <dpl/test/test_failed.h>
52 #include <dpl/test/test_ignored.h>
53 #include <dpl/test/test_result.h>
54 #include <dpl/test/test_results_collector.h>
60 typedef std::map<std::string, TestResultsCollectorBasePtr>
61 TestResultsCollectors;
62 TestResultsCollectors m_collectors;
64 std::string m_startTestId;
67 std::queue<std::string> m_failReason;
71 m_currentTestCase(nullptr)
73 , m_allowChildLogs(false)
75 , m_firstDeferredExceptionType(DeferredExceptionType::DEFERRED_FAILED)
78 void beginPerformance(std::chrono::system_clock::duration maxDurationInMicroseconds);
79 void endPerformance();
80 void setCurrentTestCasePerformanceResult(const PerformanceResultPtr &performance);
81 ConstPerformanceResultPtr getCurrentTestCasePerformanceResult();
83 void addFailReason(const std::string &reason);
88 typedef std::list<TestCasePtr> TestCaseList;
89 typedef std::map<std::string, TestCaseList> TestCaseGroupMap;
90 typedef std::set<TestCasePtr> TestCaseSet;
92 TestCaseGroupMap m_testGroups;
93 TestCaseSet m_testCaseSet;
95 TestCasePtr m_currentTestCase;
98 std::string m_currentGroup;
100 // Terminate without any logs.
101 // Some test requires to call fork function.
102 // Child process must not produce any logs and should die quietly.
104 bool m_allowChildLogs;
107 void InvalidArgs(const std::string& message = "Invalid arguments!");
110 bool filterGroupsByXmls(const std::vector<std::string> & files);
111 bool filterByXML(std::map<std::string, bool> & casesMap);
112 void normalizeXMLTag(std::string& str, const std::string& testcase);
114 void RunTestCase(TestCasePtr testCase);
116 void setCurrentTestCase(TestCasePtr testCase);
117 TestCasePtr getCurrentTestCase();
121 std::string getConcatedFailReason(const std::string &reason);
123 void CollectResult(const std::string& id, const TestResult &result);
126 void InitGroup(const char* name);
127 void RegisterTest(TestCasePtr testCase);
128 int ExecTestRunner(int argc, char *argv[]);
129 typedef std::vector<std::string> ArgsList;
130 int ExecTestRunner(ArgsList args);
131 // The runner will terminate as soon as possible (after current test).
133 bool GetAllowChildLogs();
135 void deferFailedException(const DPL::Test::TestFailed &ex);
136 void deferIgnoredException(const DPL::Test::TestIgnored &ex);
141 std::vector<std::string> m_deferredExceptionsMessages;
142 std::size_t m_deferDeepness;
143 enum DeferredExceptionType {
146 } m_firstDeferredExceptionType;
147 DPL::Test::TestFailed m_firstDeferredFail;
148 DPL::Test::TestIgnored m_firstDeferredIgnore;
151 typedef DPL::Singleton<TestRunner> TestRunnerSingleton;
153 TestResult::FailStatus TryCatch(const std::function<void(void)> &func, std::string &reason);
158 #define RUNNER_TEST_GROUP_INIT(GroupName) \
159 static int Static##GroupName##Init() \
161 DPL::Test::TestRunnerSingleton::Instance().InitGroup(#GroupName); \
164 const int DPL_UNUSED Static##GroupName##InitVar = \
165 Static##GroupName##Init();
167 #define RUNNER_TEST(Proc, ...) \
168 void Proc(std::tuple<__VA_ARGS__> &optionalArgsTuple); \
169 static int Static##Proc##Init() \
171 DPL::Test::TestRunnerSingleton::Instance().RegisterTest( \
172 new DPL::Test::TestCaseExtended<__VA_ARGS__>(#Proc, &Proc)); \
175 const int DPL_UNUSED Static##Proc##InitVar = Static##Proc##Init(); \
176 void Proc(std::tuple<__VA_ARGS__> &optionalArgsTuple DPL_UNUSED)
181 * Use them to create assertions in test cases. To do that put them inside test
182 * body. Failing assertion indicates failing test.
185 #define RUNNER_ASSERT_MSG(test, message) \
190 std::ostringstream assertMsg; \
191 assertMsg << message << DPL::gdbbacktrace(); \
192 DPL::Test::TestFailed e(#test, \
196 if (!std::uncaught_exception()) \
198 DPL::Test::TestRunnerSingleton::Instance().addFailReason(e.GetMessage()); \
202 #define RUNNER_ASSERT_ERRNO_MSG(test, message) \
207 const char *err = strerror(errno); \
208 std::ostringstream assertMsg; \
209 assertMsg << message; \
210 if (!assertMsg.str().empty()) \
212 assertMsg << err << DPL::gdbbacktrace(); \
213 DPL::Test::TestFailed e(#test, \
217 if (!std::uncaught_exception()) \
219 DPL::Test::TestRunnerSingleton::Instance().addFailReason(e.GetMessage()); \
223 #define RUNNER_ASSERT_ERRNO(test) \
224 RUNNER_ASSERT_ERRNO_MSG(test, "")
226 #define RUNNER_FAIL_MSG(message) \
227 RUNNER_ASSERT_MSG(false, message)
229 #define RUNNER_ASSERT(test) \
230 RUNNER_ASSERT_MSG(test, "")
235 * When test reaches this macro call, its furhter code will be ignored.
236 * To ignore whole test, put this macro call at the beginning of this tests
240 #define RUNNER_IGNORED_MSG(message) \
243 std::ostringstream assertMsg; \
244 assertMsg << message; \
245 throw DPL::Test::TestIgnored(assertMsg.str()); \
251 * Use these macros to do the time measurement. The first macro will start time measurement,
252 * the second will gather the result. These macros can be used only once per test-case.
253 * The result of time measurement will be displayed only if the test will pass.
254 * Notice that these macros will work only if will be used in parent process. If these
255 * macros will be used in child process then there will be no time measure results printed.
256 * This macro in multiprocess tests has effect only if used in parent process. This macro
257 * used in child process in multiprocess test has no effect.
258 * The precision of measurement is 1 microsecond - the smallest time value that can be
259 * measured is 0.000001s.
260 * The time measure results will be printed only specific output format:
266 #define RUNNER_PERF_TEST_BEGIN(maxTime) \
268 DPL::Test::TestRunnerSingleton::Instance().beginPerformance( \
269 std::chrono::microseconds{static_cast<long long int>(maxTime*1000000.0)}); \
272 #define RUNNER_PERF_TEST_END() \
274 DPL::Test::TestRunnerSingleton::Instance().endPerformance(); \
280 * Use these macros to print error messages during test run time
283 #define RUNNER_ERROR_MSG(message) \
285 std::cerr << DPL::Colors::Text::RED_BEGIN << message \
286 << DPL::Colors::Text::RED_END << std::endl; \
292 * Use them to defer fails and ignores in test cases.
293 * Some code constructions disallow to throw. Such places can be surrounded
294 * with RUNNER_DEFER_SCOPE macro. RUNNER_DEFER_TRYCATCH macro can be used to catch possibly thrown
295 * exceptions within such scope. Possibly catched exceptions will be rethrown
296 * when leaving RUNNER_DEFER_SCOPE scope.
297 * Macros can be safely nested.
301 #define RUNNER_DEFER_TRYCATCH(scope) \
307 catch (const DPL::Test::TestFailed &ex) \
309 DPL::Test::TestRunnerSingleton::Instance().deferFailedException(ex); \
311 catch (const DPL::Test::TestIgnored &ex) \
313 DPL::Test::TestRunnerSingleton::Instance().deferIgnoredException(ex); \
317 #define RUNNER_DEFER_SCOPE(scope) \
319 DPL::Test::TestRunnerSingleton::Instance().deferBegin(); \
321 DPL::Test::TestRunnerSingleton::Instance().deferEnd(); \
324 #endif // DPL_TEST_RUNNER_H