Menu

Fflushing output with TextUi::TestRunner

Help
wwhyte
2007-11-15
2013-04-22
  • wwhyte

    wwhyte - 2007-11-15

    Hi,

    I'm using cppunit to run a suite of about 700 unit tests. The
    test runner is of type CppUnit::TextUi::TestRunner. The runner
    prints a period for every test that successfully completes, an
    "E" for errors, and an "F" for failed assertions.

    However, it only outputs to the screen at the end of the entire
    set of tests.

    Is it possible to output after each test, so that the person
    running the test gets some assurance that the test app is
    still running and hasn't just hung? In other words, is there
    an output handler that fflush'es after every test? If not,
    where's the best place to look for guidance on how to write one?

    First poster -- thanks for your help. I've searched the archives
    for "fflush" and haven't found anything -- maybe I should be using
    a more C++ish keyword, but I'm more familiar with C...

    Cheers,

    William

     
    • wwhyte

      wwhyte - 2007-11-17

      Here's how I was able to fix this, for the benefit of anyone else who's interested.

      FlushingTextTestProgressListener.cpp:

      ====

      #include "FlushingTextTestProgressListener.h"
      #include <cppunit/TestFailure.h>
      #include <cppunit/portability/Stream.h>

      using namespace CppUnit;

      FlushingTextTestProgressListener::FlushingTextTestProgressListener() {}

      FlushingTextTestProgressListener::~FlushingTextTestProgressListener() {}

      void FlushingTextTestProgressListener::startTest( Test *test ) {
            stdCOut() << "."; stdCOut().flush();
      }

      void FlushingTextTestProgressListener::addFailure( const TestFailure &failure )
      {
            stdCOut() << (failure.isError() ? "E" : "F" ); stdCOut().flush();
      }

      void FlushingTextTestProgressListener::endTestRun( Test *test,
                         TestResult *eventManager ) {
            stdCOut() << "\n"; stdCOut().flush();
      }

      ====

      FlushingTextTestProgressListener.h:

      ====

      #ifndef CPPUNIT_FLUSHINGTEXTTESTPROGRESSLISTENER_H
      #define CPPUNIT_FLUSHINGTEXTTESTPROGRESSLISTENER_H

      #include <cppunit/TestListener.h>

      class FlushingTextTestProgressListener :
        public CppUnit::TestListener
      {
      public:
        FlushingTextTestProgressListener();

        ~FlushingTextTestProgressListener();

        void startTest( CppUnit::Test *test );

        void addFailure( const CppUnit::TestFailure &failure );

        void endTestRun( CppUnit::Test *test,
                         CppUnit::TestResult *eventManager );

      };

      #endif // ndef CPPUNIT_FLUSHINGTEXTTESTPROGRESSLISTENER_H

      ====

      Then in my main test, include the following include lines:

      ======

      #include <cppunit/ui/text/TestRunner.h>
      #include <cppunit/TestResult.h>
      #include "FlushingTextTestProgressListener.h"

      ======

      and the following code lines:

      =====

          CppUnit::TextUi::TestRunner runner;

          // add tests...

          FlushingTextTestProgressListener progress;
          runner.eventManager().addListener( &progress );

          bool rval = runner.run();

      =====

      Hope this is of use to someone.

      Cheers,

      William

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.