Rajinder Yadav - Windows C++ Development Tools & Resources :Design, Code, Test, Deploy

// Source: UnitTestRunner.h
// Author: Rajinder Yadav
// Date:   June 30, 2004
//
// Copyright (c) Rajinder Yadav 2004, 2007
//
// web:   http://devmentor.org
// email: rajinder@devmentor.org
//

#ifndef _UnitTestRunner_h_
#define _UnitTestRunner_h_

// warning C4251: 'UnitTestRunner::m_ObserverList' : class 'std::vector<_Ty>' 
// needs to have dll-interface to be used by clients of class 'UnitTestRunner'
#pragma warning(disable:4251

// forward declaration
struct IUnitTestRunner;
struct 
IUnitTestObserver;

class 
UTCORE_API UnitTestRunner : public IUnitTestRunner
{
protected:
   friend 
class UnitTestRunner_UnitTest;

   
// optimized in most likely, most occuring order
   
enum TESTCASE_EVENT 
   { 
      TC_EVENT_PASSED,
      TC_EVENT_START,
      TC_EVENT_END,
      TC_EVENT_FAILED,
      TC_EVENT_EXCEPTION
   }
;

   
std::vector<IUnitTestObserver*> m_ObserverList;

   
std::wstring m_strTestClass;   // unit test class name
   
std::wstring m_strTestCase;    // unit test case name

   
std::wstringstream m_strMessageStream;

   long 
m_nTestCases;   // count, total running test cases
   
long m_nFailed;      // count, failed test cases
   
long m_nExceptions;  // count, unhandeled exceptions


   //------------------------------
   // (internal) framework methods
   //------------------------------
   
inline void IncTestCount()   { ++m_nTestCases;   }
   
inline void IncFailed()      { ++m_nFailed;      }
   
inline void IncException()   { ++m_nExceptions;  }

public :

   UnitTestRunner(
const wchar_tconst name);
   virtual 
~UnitTestRunner();

   
//---------------------------
   // interface IUnitTestRunner
   //---------------------------
   
virtual void Setup() { }
   
virtual void CleanUp() { }
   
virtual void RunTest() 0;

   
//--------------
   // general APIs
   //--------------
   
inline long TestCountIs() const      return m_nTestCases;  }
   
inline long FailedCountIs() const    return m_nFailed;     }
   
inline long ExceptionCountIs() const return m_nExceptions}

   
inline std::wstring ClassNameIs() const return m_strTestClass}

   
inline void TestCaseIs(std::wstring name) { m_strTestCase name}
   
inline std::wstring TestCaseIs() const return m_strTestCase}

   
inline void ClearTestMsg() { m_strMessageStream.rdbuf()->str( L"" )}
   
inline void AppendTestMsg(std::wstring msg) { m_strMessageStream << msg}
   
inline std::wstring TestMsgIs() const return m_strMessageStream.str()}

   
inline void AddObserver(IUnitTestObserver& observer)
   {
      m_ObserverList.push_back(&observer)
;
   
}

   
inline void RemoveObserver(IUnitTestObserver& observer)
   {
      std::vector<IUnitTestObserver*>::iterator it 
=
      
std::remove( m_ObserverList.begin(), m_ObserverList.end(), &observer);
      if
( it !m_ObserverList.end() ) {
         m_ObserverList.erase( it )
;
      
}
   }

   
inline void Reset() 
   {
      m_nTestCases  
0;
      
m_nFailed     0;
      
m_nExceptions 0;
      
ClearTestMsg();
   
}

   
void NofityObserver(TESTCASE_EVENT eResult);
};

#endif 
// _UnitTestRunner_h_
Back

Copyright © 2007 Rajinder Yadav, All rights reserved