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;
53 typedef void (*TestCase)();
61 bool operator <(const TestCaseStruct &other) const
63 return name < other.name;
66 bool operator ==(const TestCaseStruct &other) const
68 return name == other.name;
71 TestCaseStruct(const std::string &n, TestCase p) :
77 typedef std::list<TestCaseStruct> TestCaseStructList;
78 typedef std::map<std::string, TestCaseStructList> TestCaseGroupMap;
79 TestCaseGroupMap m_testGroups;
81 typedef std::set<std::string> SelectedTestNameSet;
82 SelectedTestNameSet m_selectedTestNamesSet;
83 typedef std::set<std::string> SelectedTestGroupSet;
84 SelectedTestGroupSet m_selectedTestGroupSet;
85 std::string m_currentGroup;
87 DPL::Atomic m_totalAssertions;
89 // Terminate without any logs.
90 // Some test requires to call fork function.
91 // Child process must not produce any logs and should die quietly.
94 void InvalidArgs(const std::string& message = "Invalid arguments!");
97 enum Status { FAILED, IGNORED, PASS };
99 Status RunTestCase(const TestCaseStruct& testCase);
103 void CollectResult(const std::string& id,
104 const std::string& description,
105 const TestResultsCollectorBase::FailStatus::Type status
106 = TestResultsCollectorBase::FailStatus::NONE,
107 const std::string& reason = std::string());
113 std::string m_message;
119 //! \brief Failed test message creator
121 //! \param[in] aTest string for tested expression
122 //! \param[in] aFile source file name
123 //! \param[in] aLine source file line
124 //! \param[in] aMessage error message
125 TestFailed(const char* aTest,
128 const std::string &aMessage);
130 TestFailed(const std::string &message);
132 std::string GetMessage() const
141 std::string m_message;
147 Ignored(const std::string &message) :
151 std::string GetMessage() const
157 void MarkAssertion();
159 void RegisterTest(const char *testName, TestCase proc);
160 void InitGroup(const char* name);
162 int ExecTestRunner(int argc, char *argv[]);
163 typedef std::vector<std::string> ArgsList;
164 int ExecTestRunner(const ArgsList& args);
165 bool getRunIgnored() const;
166 // The runner will terminate as soon as possible (after current test).
170 typedef DPL::Singleton<TestRunner> TestRunnerSingleton;
174 #define RUNNER_TEST_GROUP_INIT(GroupName) \
175 static int Static##GroupName##Init() \
177 DPL::Test::TestRunnerSingleton::Instance().InitGroup(#GroupName); \
180 const int DPL_UNUSED Static##GroupName##InitVar = \
181 Static##GroupName##Init();
183 #define RUNNER_TEST(Proc) \
185 static int Static##Proc##Init() \
187 DPL::Test::TestRunnerSingleton::Instance().RegisterTest(#Proc, &Proc); \
190 const int DPL_UNUSED Static##Proc##InitVar = Static##Proc##Init(); \
193 //! \brief Returns base name for path
195 #define RUNNER_ASSERT_MSG(test, message) \
198 DPL::Test::TestRunnerSingleton::Instance().MarkAssertion(); \
202 std::ostringstream assertMsg; \
203 assertMsg << message; \
204 throw DPL::Test::TestRunner::TestFailed(#test, \
211 #define RUNNER_ASSERT(test) RUNNER_ASSERT_MSG(test, "")
213 #define RUNNER_FAIL RUNNER_ASSERT(false)
215 #define RUNNER_IGNORED_MSG(message) do { std::ostringstream assertMsg; \
216 assertMsg << message; \
217 throw DPL::Test::TestRunner::Ignored( \
221 #endif // DPL_TEST_RUNNER_H