Creating the unit test project

To create the unit test project and some tests, do the following:

  1. Create a test directory under the example project: C:\vidyasvn\MapEx\test.

  2. Copy \Tutorial\group\ExtraTestBuildTasks.bldmake and \Tutorial\test\testgen.bat from the test framework tutorial application to the test directory.

  3. Create a minimal test suite to the file TestHeader.h: Each test prefixed method is treated as a test case; the test target is added as a class variable, and the test class (aka fixture) is derived from CxxTest::TestSuite.

    #ifndef TESTHEADER_H
    #define TESTHEADER_H
    
    #include "TestSuite.h"
    
    // forward declaration
    class CMapExampleSmsEngine;
    class MSmsEngineObserver;
    
    class CMapExampleSmsEngineTest : public CxxTest::TestSuite
    {
    public:
    	CMapExampleSmsEngineTest(const TDesC8& aSuiteName) : CxxTest::TestSuite(aSuiteName){}
    
    private: // from CxxTest::TestSuite
    	virtual void setUp();
    	virtual void tearDown();
    
    public:
    	void testParseMsgCoordinates();
    	void testParseMsgRequestType();
    	void testParseMsgUid();
    	void testSendMessage();
    	void testSendMessageExceptions();
    
    private: // data
    	MSmsEngineObserver* iObserver;
    	CMapExampleSmsEngine* iTarget;
    };
    
    #endif // TESTHEADER_H

    Note: Perl script, which generates a test suite from this header, requires that the class definition line and constructor line are not broken with a line feed.

  4. Create an empty implementation for the unit tests to the file TestSource.cpp:

    #include "TestDriver.h"
    #include "Logger.h"
    
    void CMapExampleSmsEngineTest::setUp(){}
    void CMapExampleSmsEngineTest::tearDown(){}
    void CMapExampleSmsEngineTest::testParseMsgCoordinates(){}
    void CMapExampleSmsEngineTest::testParseMsgRequestType(){}
    void CMapExampleSmsEngineTest::testParseMsgUid(){}
    void CMapExampleSmsEngineTest::testSendMessage(){}
    void CMapExampleSmsEngineTest::testSendMessageExceptions(){}
    

    Note: TestDriver.h is a file that is generated on the fly from the TestHeader.h during the build process.

  5. Create a minimal Symbian makefile for the test: SymbianOSUnit.mmp:

    // test class definitions & implementations
    USERINCLUDE .
    SOURCEPATH  .
    SOURCE      TestSource.cpp
    
    // test target class definitions & implementations
    USERINCLUDE ..\inc
    SOURCEPATH..\src
    // SOURCE   CMapExampleSMSEngine.cpp // Our tests don't test actual class yet
    
    // libraries the test target depends on
    LIBRARY etext.lib
    
    // include SymbianOSUnit mmp file from proper
    // directory depending on relative path and target platform
    #include "..\..\SymbianOSUnit\SymbianOSUnitApp\group\s60_3rd\SymbianOSUnit.source"
    

    Note: CMapExampleSMSEngine source is commented out because our test does not test it yet, and that component has dependencies to other classes, which need to be handled when the target is really being tested.

  6. Create a bld.inf file for the test project:

    PRJ_MMPFILES
    makefile ExtraTestBuildTasks.bldmake
    SymbianOSUnit.mmp
    

After following the steps described above, the example directory with tests should look like the figure below.

Figure 13: Project directory structure