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.
17 * @file test_runner.cpp
18 * @author Przemyslaw Dobrowolski (p.dobrowolsk@samsung.com)
19 * @author Lukasz Wrzosek (l.wrzosek@samsung.com)
21 * @brief This file is the implementation file of test runner
24 #include <dpl/test/test_runner.h>
25 #include <dpl/test/test_results_collector.h>
26 #include <dpl/exception.h>
27 #include <dpl/scoped_free.h>
28 #include <dpl/foreach.h>
29 #include <dpl/log/log.h>
30 #include <dpl/colors.h>
38 #include <dpl/utils/wrt_global_settings.h>
40 #include <dpl/singleton_impl.h>
41 IMPLEMENT_SINGLETON(DPL::Test::TestRunner)
48 namespace // anonymous
51 std::string BaseName(std::string aPath)
53 ScopedFree<char> path(strdup(aPath.c_str()));
54 if (NULL == path.Get())
56 throw std::bad_alloc();
58 char* baseName = basename(path.Get());
59 std::string retValue = baseName;
63 } // namespace anonymous
65 //! \brief Failed test message creator
67 //! \param[in] aTest string for tested expression
68 //! \param[in] aFile source file name
69 //! \param[in] aLine source file line
70 //! \param[in] aMessage error message
71 TestRunner::TestFailed::TestFailed(const char* aTest,
74 const std::string &aMessage)
76 std::ostringstream assertMsg;
77 assertMsg << "[" << BaseName(aFile) << ":" << aLine
78 << "] Assertion failed ("
79 << aTest << ") " << aMessage;
80 m_message = assertMsg.str();
83 void TestRunner::RegisterTest(const char *testName, TestCase proc)
85 m_testGroups[m_currentGroup].push_back(TestCaseStruct(testName, proc));
88 void TestRunner::InitGroup(const char* name)
90 m_currentGroup = name;
94 TestRunner::Status TestRunner::RunTestCase(const TestCaseStruct& testCase)
100 catch (const TestFailed &e)
102 // Simple test failure
103 CollectResult(testCase.name,
105 TestResultsCollectorBase::FailStatus::FAILED,
109 catch (const Ignored &e)
112 // Simple test have to be implemented
113 CollectResult(testCase.name,
115 TestResultsCollectorBase::FailStatus::IGNORED,
121 catch (const DPL::Exception &e)
123 // DPL exception failure
124 CollectResult(testCase.name,
126 TestResultsCollectorBase::FailStatus::INTERNAL,
127 "DPL exception:" + e.GetMessage());
131 catch (const std::exception &)
133 // std exception failure
134 CollectResult(testCase.name,
136 TestResultsCollectorBase::FailStatus::INTERNAL,
143 // Unknown exception failure
144 CollectResult(testCase.name,
146 TestResultsCollectorBase::FailStatus::INTERNAL,
147 "unknown exception");
152 CollectResult(testCase.name,
154 TestResultsCollectorBase::FailStatus::NONE);
160 void TestRunner::RunTests()
162 using namespace DPL::Colors::Text;
165 std::for_each(m_collectors.begin(),
167 [](const TestResultsCollectors::value_type& collector)
169 collector.second->Start();
172 fprintf(stderr, "%s%s%s\n", GREEN_BEGIN, "Running tests...", GREEN_END);
173 FOREACH(group, m_testGroups) {
174 TestCaseStructList list = group->second;
177 m_collectors.begin(),
179 [&group](const TestResultsCollectors::value_type& collector)
182 CollectCurrentTestGroupName(group->first);
186 for (TestCaseStructList::const_iterator iterator = list.begin();
187 iterator != list.end();
190 TestCaseStruct test = *iterator;
191 if (m_startTestId == test.name)
194 if (m_startTestId.empty()) {
201 std::for_each(m_collectors.begin(),
203 [](const TestResultsCollectors::value_type& collector)
205 collector.second->Finish();
209 fprintf(stderr, "%s%s%s\n\n", GREEN_BEGIN, "Finished", GREEN_END);
212 void TestRunner::CollectResult(
213 const std::string& id,
214 const std::string& description,
215 const TestResultsCollectorBase::FailStatus::Type status,
216 const std::string& reason)
218 std::for_each(m_collectors.begin(),
220 [&](const TestResultsCollectors::value_type& collector)
222 collector.second->CollectResult(id,
229 void TestRunner::Banner()
231 using namespace DPL::Colors::Text;
245 void TestRunner::InvalidArgs(const std::string& message)
247 using namespace DPL::Colors::Text;
255 void TestRunner::Usage()
257 fprintf(stderr, "Usage: runner [options]\n\n");
258 fprintf(stderr, "Output type:\n");
259 fprintf(stderr, " --output=<output type> --output=<output type> ...\n");
260 fprintf(stderr, "\n possible output types:\n");
261 FOREACH (type, TestResultsCollectorBase::GetCollectorsNames()) {
262 fprintf(stderr, " --output=%s\n", type->c_str());
264 fprintf(stderr, "\n example:\n");
265 fprintf(stderr, " test-binary --output=text --output=xml --file=output.xml\n\n");
266 fprintf(stderr, "Other parameters:\n");
268 " --regexp='regexp'\t Only selected tests"
269 " which names match regexp run\n\n");
270 fprintf(stderr, " --start=<test id>\tStart from concrete test id");
271 fprintf(stderr, " --group=<group name>\t Run tests only from one group\n");
272 fprintf(stderr, " --runignored\t Run also ignored tests\n");
273 fprintf(stderr, " --list\t Show a list of Test IDs\n");
274 fprintf(stderr, " --listgroups\t Show a list of Test Group names \n");
275 fprintf(stderr, " --listingroup=<group name>\t Show a list of Test IDS in one group\n");
276 fprintf(stderr, " --help\t This help\n\n");
277 std::for_each(m_collectors.begin(),
279 [](const TestResultsCollectors::value_type& collector)
282 "Output %s has specific args:\n",
283 collector.first.c_str());
287 CollectorSpecificHelp().c_str());
289 fprintf(stderr, "For bug reporting, please write to:\n");
290 fprintf(stderr, "<p.dobrowolsk@samsung.com>\n");
293 int TestRunner::ExecTestRunner(int argc, char *argv[])
295 std::vector<std::string> args;
296 for (int i = 0; i < argc; ++i)
298 args.push_back(argv[i]);
300 return ExecTestRunner(args);
303 void TestRunner::MarkAssertion()
308 int TestRunner::ExecTestRunner(const ArgsList& value)
310 m_runIgnored = false;
311 ArgsList args = value;
312 // Parse command line
313 if (args.size() == 1)
320 args.erase(args.begin());
322 bool showHelp = false;
324 TestResultsCollectorBasePtr currentCollector;
326 // Parse each argument
329 std::string arg = *it;
330 const std::string regexp = "--regexp=";
331 const std::string output = "--output=";
332 const std::string groupId = "--group=";
333 const std::string runIgnored = "--runignored";
334 const std::string listCmd = "--list";
335 const std::string startCmd = "--start=";
336 const std::string listGroupsCmd = "--listgroups";
337 const std::string listInGroup = "--listingroup=";
339 if (currentCollector)
341 if (currentCollector->ParseCollectorSpecificArg(arg))
347 if (arg.find(startCmd) == 0)
349 arg.erase(0, startCmd.length());
350 FOREACH(group, m_testGroups) {
351 FOREACH(tc, group->second) {
352 if (tc->name == arg) {
357 if (!m_startTestId.empty()) break;
359 if (!m_startTestId.empty()) continue;
361 fprintf(stderr, "Start test id has not been found\n");
365 else if (arg.find(groupId) == 0)
367 arg.erase(0, groupId.length());
368 TestCaseGroupMap::iterator found = m_testGroups.find(arg);
369 if (found != m_testGroups.end()) {
370 std::string name = found->first;
371 TestCaseStructList newList = found->second;
372 m_testGroups.clear();
373 m_testGroups[name] = newList;
375 fprintf(stderr, "Group %s not found\n", arg.c_str());
381 else if (arg == runIgnored)
385 else if (arg == listCmd)
387 FOREACH(group, m_testGroups) {
388 FOREACH(test, group->second) {
389 printf("ID:%s:%s\n", group->first.c_str(), test->name.c_str());
394 else if (arg == listGroupsCmd)
396 FOREACH(group, m_testGroups) {
397 printf("GR:%s\n", group->first.c_str());
401 else if (arg.find(listInGroup) == 0)
403 arg.erase(0, listInGroup.length());
404 FOREACH(test, m_testGroups[arg]) {
405 printf("ID:%s\n", test->name.c_str());
409 else if (arg == "--help")
411 else if (arg.find(output) == 0)
413 arg.erase(0, output.length());
414 if (m_collectors.find(arg) != m_collectors.end())
416 InvalidArgs("Multiple outputs of the same type are not supported!");
420 currentCollector.Reset(TestResultsCollectorBase::Create(arg));
421 if (!currentCollector) {
422 InvalidArgs("Unsupported output type!");
426 m_collectors[arg] = currentCollector;
428 else if (arg.find(regexp) == 0)
430 arg.erase(0, regexp.length());
431 if (arg.length() == 0)
438 if (arg[0] == '\'' && arg[arg.length() - 1] == '\'')
441 arg.erase(arg.length() - 1);
444 if (arg.length() == 0)
451 pcrecpp::RE re(arg.c_str());
452 FOREACH(group, m_testGroups) {
453 TestCaseStructList newList;
454 FOREACH(iterator, group->second)
456 if (re.PartialMatch(iterator->name))
458 newList.push_back(*iterator);
461 group->second = newList;
472 currentCollector.Reset();
481 if (m_collectors.empty())
483 TestResultsCollectorBasePtr collector(
484 TestResultsCollectorBase::Create("text"));
485 m_collectors["text"] = collector;
488 for (auto it = m_collectors.begin(); it != m_collectors.end(); ++it)
490 if (!it->second->Configure())
492 fprintf(stderr, "Could not configure selected output");
503 bool TestRunner::getRunIgnored() const