Click here to Skip to main content
15,885,182 members
Articles / Mobile Apps

Unit Testing Framework for eVC++ Applications

Rate me:
Please Sign up or sign in to vote.
4.63/5 (13 votes)
17 Oct 2004CPOL6 min read 85.1K   246   28  
A unit testing framework for eVC++ applications, and its usage.
/******************************************************************************
 * File Name:		TestRunner.cpp
 * Description:		This file contains the definitions of methods of 
 *					CTestRunner class.
 * Date:			12/8/2004
 * Author:			AbdulMunaf Chhatra
 * E-Mail:			a.chhatra@popnet.co.in
 *
 * Warning:
 * This code is provided as is without any guarantee or warranty, implicit or 
 * explicit. If this code damages your system then the author is not 
 * responsible for it. This code is free to use with the permission of author. 
 * If you want to use this code then please send me an email at 
 * a.chhatra@popnet.co.in for permission, I will gladly give the permission. 
 * If you make any changes in this code, then feel free to add your name here, 
 * but this notice should appear in this code.
 ******************************************************************************
 * ------------------------------
 * Version History
 * ------------------------------
 * Version	:  1.0
 * Author	:  AbdulMunaf Chhatra
 * Date    	:  12/8/2004
 * Comment 	:  Initial release
 * ------------------------------
 *****************************************************************************/
 
#include "stdafx.h"
#include "TestRunner.h"
#include "WCEUnitDialog.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CTestRunner::CTestRunner()
{
    m_bInitOnce     = FALSE;
    m_pTestDialog   = NULL;
    m_TestSuiteList.RemoveAll();
}

CTestRunner::~CTestRunner()
{
    m_pTestDialog   = NULL;
    m_TestSuiteList.RemoveAll();
}


/********************************************************************
 * Purpose:       Adds the test suite to the test suite list.
 *
 * Return value:  none
 *
 * testSuite:     Pointer to the test case to be added to the list.
 *
 * Date:          2004/03/27	
 ********************************************************************/
void CTestRunner::AddTestSuite(CTestSuite *testSuite)
{
	m_TestSuiteList.AddTail(testSuite);

    //Call CreateTestSuite() of the newly added test suite
    //to add all the test cases to its test case list.
	m_TestSuiteList.GetTail()->CreateTestSuite();
}	


/********************************************************************
 * Purpose:       Initiates the dialog to test run.
 *
 * Return value:  none
 *
 * Date:          2004/03/27	
 ********************************************************************/
void CTestRunner::Start()
{
    CWCEUnitDialog wceDlg;
    int nListCount;
    POSITION pos;

    pos = m_TestSuiteList.GetHeadPosition();
    nListCount = m_TestSuiteList.GetCount();
    for(int i = 0 ; i < nListCount ; i++)
    {
        wceDlg.SetTestSuiteList(m_TestSuiteList.GetNext(pos));
    }
    m_pTestDialog = &wceDlg;
    wceDlg.SetTestRunnerPointer(this);

    wceDlg.DoModal();

    m_pTestDialog = NULL;
}


/********************************************************************
 * Purpose:       Runs the specified test case of specified test suite
 *
 * Return value:  none
 *
 * nTestSuitIdx:  Index of the test suite whose test case to be run.
 *
 * nTestCaseIdx:  Index of the test case which is to be run.
 *
 * Date:          2004/03/27	
 ********************************************************************/
void CTestRunner::RunTestCase(int nTestSuitIdx, int nTestCaseIdx)
{
    POSITION    posTestSuite = m_TestSuiteList.FindIndex(nTestSuitIdx);
    TEST_RESULT trResult;
    
    
    CString strSuiteName = m_TestSuiteList.GetAt(posTestSuite)->GetTestSuiteName();
    CString strTestName  = m_TestSuiteList.GetAt(posTestSuite)->GetTestCaseName(nTestCaseIdx);
    
    if(!m_bInitOnce)
    {
        m_TestSuiteList.GetAt(posTestSuite)->InitializeTest();
    }

    trResult = m_TestSuiteList.GetAt(posTestSuite)->RunTestCase(nTestCaseIdx);
    
    if(!m_bInitOnce)
    {
        m_TestSuiteList.GetAt(posTestSuite)->CleanUpTest();
    }
    
    m_pTestDialog->AddTestResult(trResult);
    //Write call back to the dialog for result.
    return ;
}


/********************************************************************
 * Purpose:       Runs all the test case of specified test suite
 *
 * Return value:  none
 *
 * nTestSuitIdx:  Index of test suite whose test to be run.
 *
 * Date:          2004/03/27	
 ********************************************************************/
void CTestRunner::RunTestSuite(int nTestSuitIdx)
{
    POSITION    posTestSuite    = m_TestSuiteList.FindIndex(nTestSuitIdx);
    int         nTestCaseCount  = m_TestSuiteList.GetAt(posTestSuite)->GetTestCount();
        
    int nCurTestCaseIdx;

    if(m_bInitOnce)
    {
        m_TestSuiteList.GetAt(posTestSuite)->InitializeTest();
    }
    
    for(nCurTestCaseIdx = 0 ; nCurTestCaseIdx < nTestCaseCount ; nCurTestCaseIdx++)
    {
        RunTestCase(nTestSuitIdx, nCurTestCaseIdx);
    }
    
    if(m_bInitOnce)
    {
        m_TestSuiteList.GetAt(posTestSuite)->CleanUpTest();
    }
    return ;
}


/********************************************************************
 * Purpose:       Runs all the test cases of all the test suites
 *
 * Return value:  none
 *
 * Date:          2004/03/27	
 ********************************************************************/
void CTestRunner::RunAll()
{
    POSITION    posCurTestSuite = m_TestSuiteList.GetHeadPosition();
    int         nListCount      = m_TestSuiteList.GetCount();

    int nCurTestSuiteIdx;
    for(nCurTestSuiteIdx = 0 ; nCurTestSuiteIdx < nListCount ; nCurTestSuiteIdx++)
    {
        RunTestSuite(nCurTestSuiteIdx);
    }
    return ;
}

/******************************************************************************
 * Description:		Runs the specified test case with test case and test suite 
 *					index. If nTestCaseIdx is -1 then it runs all the test case
 *					for nTestSuiteIdx test suite. If nTestSuiteIdx is also -1 
 *					then it runs all test cases of all test suites.
 * 
 * Parameters:
 * nTestSuiteIdx:	Index of the test suite.
 *
 * nTestCaseIdx:	Index of the test case.
 *
 * bInitOnce:		Indicates wether to run InitializeTest() and CleanUpTest()
 *					for all test cases or once for a test suite.
 * Return Value:	
 *****************************************************************************/
void CTestRunner::Run(int nTestSuiteIdx, int nTestCaseIdx, BOOL bInitOnce)
{
    m_bInitOnce = bInitOnce;

    if(nTestSuiteIdx == -1)
    {
        POSITION pos = m_TestSuiteList.GetHeadPosition();
        int     nLen = m_TestSuiteList.GetCount();
        int     nCount = 0;
        for(int i = 0 ; i < nLen ; i++)
        {
            nCount += m_TestSuiteList.GetNext(pos)->GetTestCount();
        }

        m_pTestDialog->SetMaxProgressRange(nCount);

        RunAll();
    }
    else
    {
        if(nTestCaseIdx == -1)
        {
            POSITION pos = m_TestSuiteList.FindIndex(nTestSuiteIdx);
            int nCount = m_TestSuiteList.GetAt(pos)->GetTestCount();
            
            m_pTestDialog->SetMaxProgressRange(nCount);
            RunTestSuite(nTestSuiteIdx);
        }
        else
        {
            m_pTestDialog->SetMaxProgressRange(1);

            RunTestCase(nTestSuiteIdx, nTestCaseIdx);
        }
    }   
    return;    
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
India India
A person who like to know about new things, peolple and make frineds. I strarted programming in 2000 with fortran but my first love in programming is C++. Also programmed in VB, MFC and for Windows CE.

Graduated in Physics and done masteres in Computer application. Physics and astronomy are my area of intrest. Karate is also one of my hobbies, BTW I am a Black Belt in Karate.

Comments and Discussions