testmain.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2005-2006 by Dr. Marc Boris Duerner
3  * Copyright (C) 2015 Tommi Maekitalo
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * As a special exception, you may use this file as part of a free
11  * software library without restriction. Specifically, if other files
12  * instantiate templates or use macros or inline functions from this
13  * file, or you compile this file and link it with other files to
14  * produce an executable, this file does not by itself cause the
15  * resulting executable to be covered by the GNU General Public
16  * License. This exception does not however invalidate any other
17  * reasons why the executable file might be covered by the GNU Library
18  * General Public License.
19  *
20  * This library is distributed in the hope that it will be useful,
21  * but WITHOUT ANY WARRANTY; without even the implied warranty of
22  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23  * Lesser General Public License for more details.
24  *
25  * You should have received a copy of the GNU Lesser General Public
26  * License along with this library; if not, write to the Free Software
27  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
28  */
29 #ifndef CXXTOOLS_UNIT_TESTMAIN_H
30 #define CXXTOOLS_UNIT_TESTMAIN_H
31 
32 #include <cxxtools/arg.h>
33 #include <cxxtools/unit/reporter.h>
36 #include <cxxtools/json.h>
37 #include <cxxtools/log.h>
38 #include <fstream>
39 
40 int main(int argc, char** argv)
41 {
42  log_init();
43 
45 
46  cxxtools::Arg<bool> help(argc, argv, 'h');
47  cxxtools::Arg<bool> list(argc, argv, 'l');
48  cxxtools::Arg<bool> json(argc, argv, "--json");
49  cxxtools::Arg<bool> json1(argc, argv, 'j');
50  cxxtools::Arg<bool> summary(argc, argv, 's');
51 
52  if( help || list )
53  {
54  std::cerr << "Usage: " << argv[0] << " {options} {testname}\n"
55  "Options:\n"
56  " -f <logfile> output results into file\n"
57  " -s print statistic summary\n"
58  " -j, --json print output in json format instead of standard\n"
59  "Available Tests:\n";
60  for (std::list<cxxtools::unit::Test*>::const_iterator it = app.tests().begin();
61  it != app.tests().end();
62  ++it)
63  {
64  std::cerr << " - " << (*it)->name() << '\n';
65  }
66 
67  return 0;
68  }
69 
70  cxxtools::unit::BriefReporter consoleReporter;
71  cxxtools::unit::JsonReporter jsonReporter;
72 
73  if (json || json1)
74  app.attachReporter(jsonReporter);
75  else
76  app.attachReporter(consoleReporter);
77 
78  cxxtools::Arg<const char*> file(argc, argv, 'f');
79  std::ofstream logFile;
80  cxxtools::unit::BriefReporter fileReporter;
81 
82  if (file.isSet())
83  {
84  logFile.open(file);
85  fileReporter.setOutput(logFile);
86  app.attachReporter(fileReporter);
87  }
88 
89  try
90  {
91  if (argc <= 1)
92  {
93  app.run();
94  }
95  else
96  {
97  for (int a = 1; a < argc; ++a)
98  {
99  std::string testName = argv[a];
100  app.run(testName);
101  }
102  }
103 
104  if (json || json1)
105  std::cout << cxxtools::Json(jsonReporter).beautify(true);
106 
107  if (summary)
108  {
109  std::cout << "success: " << app.success() << "\n"
110  "errors: " << app.errors() << "\n"
111  "skipped: " << app.skipped() << "\n";
112  }
113 
114  return app.errors();
115  }
116  catch(const std::exception& ex)
117  {
118  std::cerr << ex.what() << std::endl;
119  }
120 
121  return 1;
122 }
123 
124 #endif// CXXTOOLS_UNIT_TESTMAIN_H