2 * Copyright (c) 2011 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
23 #ifndef DPL_TEST_RUNNER_H
24 #define DPL_TEST_RUNNER_H
26 #include <dpl/singleton.h>
27 #include <dpl/unused.h>
28 #include <dpl/atomic.h>
29 #include <dpl/test/test_results_collector.h>
41 typedef std::map<std::string, TestResultsCollectorBasePtr>
42 TestResultsCollectors;
43 TestResultsCollectors m_collectors;
45 std::string m_startTestId;
51 , m_allowChildLogs(false)
54 typedef void (*TestCase)();
62 bool operator <(const TestCaseStruct &other) const
64 return name < other.name;
67 bool operator ==(const TestCaseStruct &other) const
69 return name == other.name;
72 TestCaseStruct(const std::string &n, TestCase p) :
78 typedef std::list<TestCaseStruct> TestCaseStructList;
79 typedef std::map<std::string, TestCaseStructList> TestCaseGroupMap;
80 TestCaseGroupMap m_testGroups;
82 typedef std::set<std::string> SelectedTestNameSet;
83 SelectedTestNameSet m_selectedTestNamesSet;
84 typedef std::set<std::string> SelectedTestGroupSet;
85 SelectedTestGroupSet m_selectedTestGroupSet;
86 std::string m_currentGroup;
88 DPL::Atomic m_totalAssertions;
90 // Terminate without any logs.
91 // Some test requires to call fork function.
92 // Child process must not produce any logs and should die quietly.
94 bool m_allowChildLogs;
97 void InvalidArgs(const std::string& message = "Invalid arguments!");
100 enum Status { FAILED, IGNORED, PASS };
102 Status RunTestCase(const TestCaseStruct& testCase);
106 void CollectResult(const std::string& id,
107 const std::string& description,
108 const TestResultsCollectorBase::FailStatus::Type status
109 = TestResultsCollectorBase::FailStatus::NONE,
110 const std::string& reason = std::string());
116 std::string m_message;
122 //! \brief Failed test message creator
124 //! \param[in] aTest string for tested expression
125 //! \param[in] aFile source file name
126 //! \param[in] aLine source file line
127 //! \param[in] aMessage error message
128 TestFailed(const char* aTest,
131 const std::string &aMessage);
133 TestFailed(const std::string &message);
135 std::string GetMessage() const
144 std::string m_message;
150 Ignored(const std::string &message) :
154 std::string GetMessage() const
160 void MarkAssertion();
162 void RegisterTest(const char *testName, TestCase proc);
163 void InitGroup(const char* name);
165 int ExecTestRunner(int argc, char *argv[]);
166 typedef std::vector<std::string> ArgsList;
167 int ExecTestRunner(const ArgsList& args);
168 bool getRunIgnored() const;
169 // The runner will terminate as soon as possible (after current test).
171 bool GetAllowChildLogs();
174 typedef DPL::Singleton<TestRunner> TestRunnerSingleton;
178 #define RUNNER_TEST_GROUP_INIT(GroupName) \
179 static int Static##GroupName##Init() \
181 DPL::Test::TestRunnerSingleton::Instance().InitGroup(#GroupName); \
184 const int DPL_UNUSED Static##GroupName##InitVar = \
185 Static##GroupName##Init();
187 #define RUNNER_TEST(Proc) \
189 static int Static##Proc##Init() \
191 DPL::Test::TestRunnerSingleton::Instance().RegisterTest(#Proc, &Proc); \
194 const int DPL_UNUSED Static##Proc##InitVar = Static##Proc##Init(); \
197 //! \brief Returns base name for path
199 #define RUNNER_ASSERT_MSG(test, message) \
202 DPL::Test::TestRunnerSingleton::Instance().MarkAssertion(); \
206 std::ostringstream assertMsg; \
207 assertMsg << message; \
208 throw DPL::Test::TestRunner::TestFailed(#test, \
215 #define RUNNER_ASSERT(test) RUNNER_ASSERT_MSG(test, "")
217 #define RUNNER_FAIL RUNNER_ASSERT(false)
219 #define RUNNER_IGNORED_MSG(message) do { std::ostringstream assertMsg; \
220 assertMsg << message; \
221 throw DPL::Test::TestRunner::Ignored( \
225 #endif // DPL_TEST_RUNNER_H