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
39 #include <dpl/atomic.h>
40 #include <dpl/availability.h>
41 #include <dpl/colors.h>
42 #include <dpl/gdbbacktrace.h>
43 #include <dpl/singleton.h>
44 #include <dpl/test/performance_result.h>
45 #include <dpl/test/test_result.h>
46 #include <dpl/test/test_results_collector.h>
52 typedef std::map<std::string, TestResultsCollectorBasePtr>
53 TestResultsCollectors;
54 TestResultsCollectors m_collectors;
56 std::string m_startTestId;
59 std::queue<std::string> m_failReason;
63 m_currentTestCase(nullptr)
65 , m_allowChildLogs(false)
68 void beginPerformance(std::chrono::system_clock::duration maxDurationInMicroseconds);
69 void endPerformance();
70 void setCurrentTestCasePerformanceResult(const PerformanceResultPtr &performance);
71 ConstPerformanceResultPtr getCurrentTestCasePerformanceResult();
73 void addFailReason(const std::string &reason);
75 typedef void (*TestCase)();
82 PerformanceResultPtr performance;
84 bool operator <(const TestCaseStruct &other) const
86 return name < other.name;
89 bool operator ==(const TestCaseStruct &other) const
91 return name == other.name;
94 TestCaseStruct(const std::string &n, TestCase p) :
100 typedef std::list<TestCaseStruct> TestCaseStructList;
101 typedef std::map<std::string, TestCaseStructList> TestCaseGroupMap;
102 TestCaseGroupMap m_testGroups;
104 TestCaseStruct * m_currentTestCase;
106 typedef std::set<std::string> SelectedTestNameSet;
107 SelectedTestNameSet m_selectedTestNamesSet;
108 typedef std::set<std::string> SelectedTestGroupSet;
109 SelectedTestGroupSet m_selectedTestGroupSet;
110 std::string m_currentGroup;
112 DPL::Atomic m_totalAssertions;
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);
143 std::string m_message;
149 //! \brief Failed test message creator
151 //! \param[in] aTest string for tested expression
152 //! \param[in] aFile source file name
153 //! \param[in] aLine source file line
154 //! \param[in] aMessage error message
155 TestFailed(const char* aTest,
158 const std::string &aMessage);
160 TestFailed(const std::string &message);
162 std::string GetMessage() const
171 std::string m_message;
177 Ignored(const std::string &message) :
181 std::string GetMessage() const
187 void MarkAssertion();
189 void RegisterTest(const char *testName, TestCase proc);
190 void InitGroup(const char* name);
192 int ExecTestRunner(int argc, char *argv[]);
193 typedef std::vector<std::string> ArgsList;
194 int ExecTestRunner(ArgsList args);
195 bool getRunIgnored() const;
196 // The runner will terminate as soon as possible (after current test).
198 bool GetAllowChildLogs();
201 typedef DPL::Singleton<TestRunner> TestRunnerSingleton;
205 #define RUNNER_TEST_GROUP_INIT(GroupName) \
206 static int Static##GroupName##Init() \
208 DPL::Test::TestRunnerSingleton::Instance().InitGroup(#GroupName); \
211 const int DPL_UNUSED Static##GroupName##InitVar = \
212 Static##GroupName##Init();
214 #define RUNNER_TEST(Proc) \
216 static int Static##Proc##Init() \
218 DPL::Test::TestRunnerSingleton::Instance().RegisterTest(#Proc, &Proc); \
221 const int DPL_UNUSED Static##Proc##InitVar = Static##Proc##Init(); \
228 * Use them to create assertions in test cases. To do that put them inside test
229 * body. Failing assertion indicates failing test.
232 #define RUNNER_ASSERT_MSG(test, message) \
235 DPL::Test::TestRunnerSingleton::Instance().MarkAssertion(); \
239 std::ostringstream assertMsg; \
240 assertMsg << message << DPL::gdbbacktrace(); \
241 DPL::Test::TestRunner::TestFailed e(#test, \
245 if (!std::uncaught_exception()) \
247 DPL::Test::TestRunnerSingleton::Instance().addFailReason(e.GetMessage()); \
251 #define RUNNER_ASSERT_ERRNO_MSG(test, message) \
254 DPL::Test::TestRunnerSingleton::Instance().MarkAssertion(); \
258 const char *err = strerror(errno); \
259 std::ostringstream assertMsg; \
260 assertMsg << message; \
261 if (!assertMsg.str().empty()) \
263 assertMsg << err << DPL::gdbbacktrace(); \
264 DPL::Test::TestRunner::TestFailed e(#test, \
268 if (!std::uncaught_exception()) \
270 DPL::Test::TestRunnerSingleton::Instance().addFailReason(e.GetMessage()); \
274 #define RUNNER_ASSERT_ERRNO(test) \
275 RUNNER_ASSERT_ERRNO_MSG(test, "")
277 #define RUNNER_FAIL_MSG(message) \
278 RUNNER_ASSERT_MSG(false, message)
280 #define RUNNER_ASSERT(test) \
281 RUNNER_ASSERT_MSG(test, "")
286 * When test reaches this macro call, its furhter code will be ignored.
287 * To ignore whole test, put this macro call at the beginning of this tests
291 #define RUNNER_IGNORED_MSG(message) \
294 std::ostringstream assertMsg; \
295 assertMsg << message; \
296 throw DPL::Test::TestRunner::Ignored(assertMsg.str()); \
302 * Use these macros to do the time measurement. The first macro will start time measurement,
303 * the second will gather the result. These macros can be used only once per test-case.
304 * The result of time measurement will be displayed only if the test will pass.
305 * Notice that these macros will work only if will be used in parent process. If these
306 * macros will be used in child process then there will be no time measure results printed.
307 * This macro in multiprocess tests has effect only if used in parent process. This macro
308 * used in child process in multiprocess test has no effect.
309 * The precision of measurement is 1 microsecond - the smallest time value that can be
310 * measured is 0.000001s.
311 * The time measure results will be printed only specific output format:
317 #define RUNNER_PERF_TEST_BEGIN(maxTime) \
319 DPL::Test::TestRunnerSingleton::Instance().beginPerformance( \
320 std::chrono::microseconds{static_cast<long long int>(maxTime*1000000.0)}); \
323 #define RUNNER_PERF_TEST_END() \
325 DPL::Test::TestRunnerSingleton::Instance().endPerformance(); \
331 * Use these macros to print error messages during test run time
334 #define RUNNER_ERROR_MSG(message) \
336 std::cerr << DPL::Colors::Text::RED_BEGIN << message \
337 << DPL::Colors::Text::RED_END << std::endl; \
340 #endif // DPL_TEST_RUNNER_H