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

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

#ifndef _UnitTestDefs_h_
#define _UnitTestDefs_h_

//-------------------------------------------------------------
// Following macros are to be use only from within 
// class UnitTestRunner or it's derivities where possible
//-------------------------------------------------------------


//-------------------------------------------------------------
// UNIT_TEST macro used by a test class to envoke 
// the named test method that returns a boolean value
// 
// return vaues:
// true  - signifies the test case passed
// false - signifies the test case failed
//-------------------------------------------------------------   

#define UNIT_TEST( _TC )                                    \
   
try {                                                    \
         Setup()
;                                           \
         IncTestCount()
;                                    \
         TestCaseIs( L
#_TC );                               \
         
if( _TC() )                                        \
         {                                                  \
            ClearTestMsg()
;                                 \
            
this->m_strMessageStream                        \
               << L
"[passed] Test case: " << ClassNameIs()  \
               << L
"::" << TestCaseIs() << L"()";           \
            NofityObserver( TC_EVENT_PASSED )
;              \
         }                                                  \
         
else                                               \
         {                                                  \
            IncFailed()
;                                    \
            ClearTestMsg()
;                                 \
            
this->m_strMessageStream                        \
            << L
"[failed] Test case: " << ClassNameIs()     \
            << L
"::" << TestCaseIs()                        \
            << L
"()";                                       \
            NofityObserver( TC_EVENT_FAILED )
;              \
         }                                                  \
         CleanUp()
;                                         \
   }                                                        \
   
catch( ... ) {                                           \
            IncException()
;                                 \
            ClearTestMsg()
;                                 \
            
this->m_strMessageStream                        \
            << L
"[except] Test case: " << ClassNameIs()     \
            << L
"::" << TestCaseIs()                        \
            << L
"() unhandled C++ exception";               \
            NofityObserver( TC_EVENT_EXCEPTION )
;           \
   }


//----------------------------------------------------------------
// UNIT_TEST_START()
//
// set the test file location then notify all unit test observer
// that the we are going to start with all the unit tests
//----------------------------------------------------------------
#define UNIT_TEST_START()            \
   NofityObserver( TC_EVENT_START )
;


//-------------------------------------------------------------
// UNIT_TEST_END()
//
// notify all the unit test observer the we have completed
// all of the unit tests
//-------------------------------------------------------------
#define UNIT_TEST_END()           \
   NofityObserver( TC_EVENT_END )
;



//-------------------------------------------------------------
// REGISTER_TESTCLASS( _CLSNAME )
//
// use this macro in the test class definition 
// to declare a default ctor that will call the base
// class ctor to save the class name and register
// the test class with the test framework
//-------------------------------------------------------------
#define REGISTER_TESTCLASS( _CLSNAME )                      \
   _CLSNAME() : UnitTestRunner( L
#_CLSNAME )                \
   {                                                        \
      UnitTestAssembly::GetInstance().RegisterTest(*
this);  \
   }


//-------------------------------------------------------------
// DECLARE_TESTRUNNER( _CLSNAME )
//
// use this macro in the source file of a test class to declare
// a global object to start the process of registering 
// the test runner class
//-------------------------------------------------------------
#define DECLARE_TESTRUNNER( _CLSNAME ) \
   _CLSNAME obj_
##_CLSNAME


//-------------------------------------------------------------
// use this macro inside the main test harner to register
// observers that will get notify of each test case result
//-------------------------------------------------------------
#define DECLARE_OBSERVER( _OBSERVER ) \
   UnitTestAssembly::GetInstance().RegisterObserver( _OBSERVER )

#endif // _UnitTestDefs_h_
Back

Copyright © 2007 Rajinder Yadav, All rights reserved