I added command line options processing to RunAllTests function.

Avaliable options:
  -s, --suite suite     Run all tests in this suite
  -t, --test test       Run only one test
  -v, --verbose         Verbose output
  -h, --help            Print this help message

Now you can start one test or tests from one suite separately. Options
-s and -t together start one test from this suite.

Please comment my code and I can correct it.
Index: src/TestReporter.h
===================================================================
--- src/TestReporter.h	(revision 207)
+++ src/TestReporter.h	(working copy)
@@ -8,12 +8,16 @@
 class TestReporter
 {
 public:
+    explicit TestReporter(bool verbose = false);
     virtual ~TestReporter();
 
     virtual void ReportTestStart(TestDetails const& test) = 0;
     virtual void ReportFailure(TestDetails const& test, char const* failure) = 0;
     virtual void ReportTestFinish(TestDetails const& test, float secondsElapsed) = 0;
     virtual void ReportSummary(int totalTestCount, int failedTestCount, int failureCount, float secondsElapsed) = 0;
+
+protected:
+    bool m_verbose;
 };
 
 }
Index: src/TestRunner.cpp
===================================================================
--- src/TestRunner.cpp	(revision 207)
+++ src/TestRunner.cpp	(working copy)
@@ -6,8 +6,11 @@
 #include "MemoryOutStream.h"
 
 #include <cstring>
+#include <getopt.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string>
 
-
 namespace UnitTest {
 
 int RunAllTests()
@@ -17,7 +20,95 @@
 	return runner.RunTestsIf(Test::GetTestList(), NULL, True(), 0);
 }
 
+void PrintUsage(const char *program)
+{
+	printf("Usage: %s [-s|--suite suite] [-t|--test test] [-v|--verbose] [-h|--help]\n", program);
+	printf("Options:\n");
+	printf("  -s, --suite suite	Run all tests in this suite\n");
+	printf("  -t, --test test	Run only one test\n");
+	printf("  -v, --verbose		Verbose output\n");
+	printf("  -h, --help		Print this help message\n");
+}
 
+class TestComparer
+{
+public:
+	TestComparer(std::string const &suite, std::string const &name):
+		m_suite(suite),
+		m_name(name)
+	{}
+
+	bool operator()(Test const *test) const
+	{
+		return compareSuite(test->m_details) && compareName(test->m_details);
+	}
+
+private:
+	bool compareSuite(TestDetails const &details) const
+	{
+		if (m_suite.empty()) return true;
+		return m_suite == details.suiteName;
+	}
+
+	bool compareName(TestDetails const &details) const
+	{
+		if (m_name.empty()) return true;
+		return m_name == details.testName;
+	}
+
+	std::string m_suite;
+	std::string m_name;
+};
+
+int RunAllTests(int argc, char *argv[])
+{
+	const struct option long_options[] = {
+		{ "suite", required_argument, NULL, 's' },
+		{ "test", required_argument, NULL, 't' },
+		{ "verbose", no_argument, NULL, 'v' },
+		{ "help", no_argument, NULL, 'h' },
+		{ NULL, 0, NULL, 0 }
+	};
+
+	std::string suite;
+	std::string test;
+	bool verbose = false;
+
+	int option;
+	int option_index;
+	while ((option = getopt_long(argc, argv, "s:t:vh", long_options, &option_index)) != -1)
+	{
+		switch (option)
+		{
+			case 's':
+				suite = optarg;
+				break;
+
+			case 't':
+				test = optarg;
+				break;
+
+			case 'v':
+				verbose = true;
+				break;
+
+			case 'h':
+				PrintUsage(argv[0]);
+				exit(0);
+				break;
+
+			case '?':
+				PrintUsage(argv[0]);
+				exit(1);
+				break;
+		}
+	}
+
+	TestReporterStdout reporter(verbose);
+	TestRunner runner(reporter);
+	return runner.RunTestsIf(Test::GetTestList(), NULL, TestComparer(suite, test), 0);
+}
+
 TestRunner::TestRunner(TestReporter& reporter)
 	: m_reporter(&reporter)
 	, m_result(new TestResults(&reporter))
Index: src/TestReporterStdout.h
===================================================================
--- src/TestReporterStdout.h	(revision 207)
+++ src/TestReporterStdout.h	(working copy)
@@ -7,6 +7,9 @@
 
 class TestReporterStdout : public TestReporter
 {
+public:
+    explicit TestReporterStdout(bool verbose = false);
+
 private:
     virtual void ReportTestStart(TestDetails const& test);
     virtual void ReportFailure(TestDetails const& test, char const* failure);
Index: src/tests/TestTestList.cpp
===================================================================
--- src/tests/TestTestList.cpp	(revision 207)
+++ src/tests/TestTestList.cpp	(working copy)
@@ -5,7 +5,6 @@
 
 namespace {
 
-
 TEST (TestListIsEmptyByDefault)
 {
     TestList list;
Index: src/tests/Main.cpp
===================================================================
--- src/tests/Main.cpp	(revision 207)
+++ src/tests/Main.cpp	(working copy)
@@ -2,7 +2,7 @@
 #include "../TestReporterStdout.h"
 
 
-int main(int, char const *[])
+int main(int argc, char *argv[])
 {
-    return UnitTest::RunAllTests();
+    return UnitTest::RunAllTests(argc, argv);
 }
Index: src/TestReporter.cpp
===================================================================
--- src/TestReporter.cpp	(revision 207)
+++ src/TestReporter.cpp	(working copy)
@@ -2,6 +2,10 @@
 
 namespace UnitTest {
 
+TestReporter::TestReporter(bool verbose)
+	: m_verbose(verbose)
+{
+}
 
 TestReporter::~TestReporter()
 {
Index: src/TestReporterStdout.cpp
===================================================================
--- src/TestReporterStdout.cpp	(revision 207)
+++ src/TestReporterStdout.cpp	(working copy)
@@ -10,6 +10,11 @@
 
 namespace UnitTest {
 
+TestReporterStdout::TestReporterStdout(bool verbose)
+	: TestReporter(verbose)
+{
+}
+
 void TestReporterStdout::ReportFailure(TestDetails const& details, char const* failure)
 {
 #if defined(__APPLE__) || defined(__GNUG__)
@@ -22,12 +27,20 @@
     printf(errorFormat, details.filename, details.lineNumber, details.testName, failure);
 }
 
-void TestReporterStdout::ReportTestStart(TestDetails const& /*test*/)
+void TestReporterStdout::ReportTestStart(TestDetails const& details)
 {
+	using namespace std;
+
+	if (m_verbose)
+		printf("Start test %s.%s.\n", details.suiteName, details.testName);
 }
 
-void TestReporterStdout::ReportTestFinish(TestDetails const& /*test*/, float)
+void TestReporterStdout::ReportTestFinish(TestDetails const& details, float secondsElapsed)
 {
+	using namespace std;
+
+	if (m_verbose)
+		printf("Finish test %s.%s. Test time %.2f seconds.\n", details.suiteName, details.testName, secondsElapsed);
 }
 
 void TestReporterStdout::ReportSummary(int const totalTestCount, int const failedTestCount,
Index: src/TestRunner.h
===================================================================
--- src/TestRunner.h	(revision 207)
+++ src/TestRunner.h	(working copy)
@@ -12,6 +12,7 @@
 class Timer;
 
 int RunAllTests();
+int RunAllTests(int argc, char *argv[]);
 
 struct True
 {
------------------------------------------------------------------------------
Start uncovering the many advantages of virtual appliances
and start using them to simplify application deployment and
accelerate your shift to cloud computing.
http://p.sf.net/sfu/novell-sfdev2dev
_______________________________________________
unittest-cpp-devel mailing list
unittest-cpp-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/unittest-cpp-devel

Reply via email to