From d04eeb4fd4c9a098423c56c2b51b8ff1a9c084c7 Mon Sep 17 00:00:00 2001 From: Slava Semushin Date: Sat, 18 Jul 2009 19:08:10 +0700 Subject: [PATCH] Fixed ticket #481 (testrunner should exit with non-zero status if one of tests fails) http://sourceforge.net/apps/trac/cppcheck/ticket/481 --- test/testrunner.cpp | 3 +-- test/testsuite.cpp | 10 +++++++++- test/testsuite.h | 3 ++- 3 files changed, 12 insertions(+), 4 deletions(-) diff --git a/test/testrunner.cpp b/test/testrunner.cpp index f86920bbf..ba5b28d98 100644 --- a/test/testrunner.cpp +++ b/test/testrunner.cpp @@ -20,7 +20,6 @@ int main(int argc, const char *argv[]) { - TestFixture::runTests((argc == 2) ? argv[1] : NULL); - return 0; + return TestFixture::runTests((argc == 2) ? argv[1] : NULL); } diff --git a/test/testsuite.cpp b/test/testsuite.cpp index 04c5d01ea..efc592d03 100644 --- a/test/testsuite.cpp +++ b/test/testsuite.cpp @@ -64,6 +64,8 @@ public: std::ostringstream TestFixture::errmsg; unsigned int TestFixture::countTests; +size_t TestFixture::fails_counter = 0; + TestFixture::TestFixture(const std::string &_name) : classname(_name) { TestRegistry::theInstance().addTest(this); @@ -105,6 +107,8 @@ void TestFixture::assertEquals(const char *filename, int linenr, const std::stri { if (expected != actual) { + ++fails_counter; + errmsg << "Assertion failed in " << filename << " at line " << linenr << std::endl << "Expected:" << std::endl << writestr(expected) << std::endl @@ -124,6 +128,8 @@ void TestFixture::assertEquals(const char *filename, int linenr, unsigned int ex void TestFixture::assertThrowFail(const char *filename, int linenr) { + ++fails_counter; + errmsg << "Assertion failed in " << filename << " at line " << linenr << std::endl << "The expected exception was not thrown" << std::endl; } @@ -144,7 +150,7 @@ void TestFixture::run(const std::string &str) run(); } -void TestFixture::runTests(const char cmd[]) +size_t TestFixture::runTests(const char cmd[]) { std::string classname(cmd ? cmd : ""); std::string testname(""); @@ -170,6 +176,8 @@ void TestFixture::runTests(const char cmd[]) std::cout << "\n\nTesting Complete\nNumber of tests: " << countTests << "\n"; std::cerr << errmsg.str(); + + return fails_counter; } void TestFixture::reportOut(const std::string & /*outmsg*/) diff --git a/test/testsuite.h b/test/testsuite.h index 2bc092aee..435640bfe 100644 --- a/test/testsuite.h +++ b/test/testsuite.h @@ -29,6 +29,7 @@ class TestFixture : public ErrorLogger private: static std::ostringstream errmsg; static unsigned int countTests; + static size_t fails_counter; protected: std::string classname; @@ -52,7 +53,7 @@ public: virtual ~TestFixture() { } static void printTests(); - static void runTests(const char cmd[]); + static size_t runTests(const char cmd[]); };