Archived:How to create a Symbian C++ project with Unit Tests
Article Metadata
Code Example
Article
Contents |
Introduction
This article presents a step-by-step guide that explains how to prepare your Symbian C++ S60 3rd Edition project to run unit tests using the SymbianOSUnit Framework [1]. First it explains how to import the SymbianOSUnit framework code into your workspace. Next, it explains how to write test suites for your code, and finally, it shows how to setup the project files to build and run your test suite.
With this guide, you can add unit test support to new and already created projects.
Step 1: Download and prepare the SymbianOSUnit Framework
SymbianOSUnit is a free and open source testing framework for Symbian OS.
You can download the SymbiaOSUnit framework here and extract it to any folder of your choice. The folder will have the structure shown in Figure 1.
Figure 1: Folders in the SymbianOSUnit package.
There are two important folders:
- SymbianOSUnit/ which holdes all the framework code.
- Tutorial/ which has an example application with important scripts and sample files.
After that, import the directory SymbianOSUnit/ as a Filesystem into your workspace, as shown in Figure 2.
- Create an empty project in your workspace:
- File->New -> Project -> General
- Import the folder SymbianOSUnit/ of the SymbianOsUnit framework as shown in Figure 2.
- File->Import->General -> File System
Figure 2: Importing the symbianosunit folder into the Carbide workspace.
After importing this folder, you have configured your workspace to support unit tests. The new project will look like this:
Figure 3: Imported SymbianOSUnit folder.
Step 2: Create your first project
Create a simple GUI S60 Application using the Carbide wizard. Then, create a class to be tested, for example:
DoStuffClass.h
#ifndef DOSTUFFCLASS_H
#define DOSTUFFCLASS_H
// INCLUDES
#include <e32std.h>
#include <e32base.h>
/**
* CDoStuffClass
*
*/
class CDoStuffClass : public CBase
{
public:
// Constructors and destructor
~CDoStuffClass();
static CDoStuffClass* NewL();
static CDoStuffClass* NewLC();
public:
/**
* Method that will be tested.
*/
TInt MethodToTest();
private:
CDoStuffClass();
void ConstructL();
};
#endif // DOSTUFFCLASS_H
DoStuffClass.cpp
#include "DoStuffClass.h"
CDoStuffClass::CDoStuffClass()
{
// No implementation required
}
CDoStuffClass::~CDoStuffClass()
{
}
CDoStuffClass* CDoStuffClass::NewLC()
{
CDoStuffClass* self = new (ELeave) CDoStuffClass();
CleanupStack::PushL(self);
self->ConstructL();
return self;
}
CDoStuffClass* CDoStuffClass::NewL()
{
CDoStuffClass* self = CDoStuffClass::NewLC();
CleanupStack::Pop(); // self;
return self;
}
void CDoStuffClass::ConstructL()
{
}
TInt CDoStuffClass::MethodToTest()
{
return 2;
}
Your created project will look like this:
Figure 4: Project that will be tested.
After creating the project, it must be prepared for unit tests.
Step 3: Creating and preparing tests
First, create a test/ folder in your project. In this folder create your TestSuite class (TestHeader.h TestSource.cpp) as follows:
TestHeader.h
#ifndef TESTHEADER_H
#define TESTHEADER_H
#include "TestSuite.h"
#include "DoStuffClass.h"
/**
Encapsulates a suite of tests.
Any member function starting 'test' is interpreted as a test to be executed.
The tests will be executed in the order they are in this file.
Note that the code for the individual tests may be in different CPP files.
*/
class CTest : public CxxTest::TestSuite
{
public:
CTest(const TDesC8& aSuiteName):CxxTest::TestSuite(aSuiteName){}
private:
void setUp();
void tearDown();
public:
void testFirstMethod();
void testFirstMethodAgain();
private:
CDoStuffClass* iMyClass;
};
#endif // TESTHEADER_H
TestSource.cpp
#include "TestHeader.h"
#include "TestDriver.h"
#include "Logger.h"
void CTest::setUp()
{
iMyClass = CDoStuffClass::NewL();
}
void CTest::tearDown()
{
if( iMyClass )
{
delete iMyClass;
}
}
void CTest::testFirstMethod()
{
TInt i = 2;
TInt j = iMyClass->MethodToTest();
TS_ASSERT_EQUALS(i,j);
}
void CTest::testFirstMethodAgain()
{
TInt i = 2;
TInt j = iMyClass->MethodToTest();
TS_ASSERT_EQUALS(i,j);
}
IMPORTANT: Do not allow Carbide to add the TestSource.cpp file into the application project .mmp file. The file (TestSource.cpp) will be added in another .mmp file. Also, do not worry about the include files for now. The symbianosunit.mmp file will handle them.
After creating your test suite, configure your symbianosunit.mmp file. SymbianOsUnit runs as another application, so it is necessary to build it. To do this, create a new .mmp file, symbianosunit.mmp, into the group/ folder.
symbianosunit.mmp
//specify test source files and libraries along with userincludes
SOURCEPATH ..\test
SOURCE TestSource.cpp
SOURCEPATH ..\src
SOURCE DoStuffClass.cpp
USERINCLUDE ..\test
USERINCLUDE ..\inc
//include SymbianOSUnit mmp file from proper directory depending on relative path and target platform
#include "..\..\SymbianOSUnit\SymbianOSUnitApp\group\s60_3rd\SymbianOSUnit.source"
Now, copy the following files from the symbianosunit framework:
- From the folder SymbianOsUnit/Tutorial/group/ copy the file ExtraTestBuildTasks.bldmake to yourProject/group folder;
- From the folder SymbianOsUnit/Tutorial/test copy the file testgen.bat to yourProject/test folder.
Then, configure your project bld.inf file as follow, adding the lines after the PRJ_TESTMMPFILES
bld.inf
/*
============================================================================
Name : bld.inf
Author :
Copyright : Your copyright notice
Description : This file provides the information required for building the
whole of a MyFirstUnitTestApp.
============================================================================
*/
PRJ_PLATFORMS
WINSCW ARMV5 GCCE
PRJ_MMPFILES
gnumakefile icons_scalable_dc.mk
gnumakefile ..\help\build_help.mk
MyFirstUnitTestApp.mmp
//LINES TO ADD ----------------------------
PRJ_TESTMMPFILES
gnumakefile extratestbuildtasks.bldmake
SymbianOSUnit.mmp
We are almost done. Now run the testgen.bat file to generate the TestDriver.h file into yourProject/test folder.
After that, your group/ and test/ folder will look like this:
Figure 5: Symbian project with all test code.
Final Step: Run your tests
Build your project as a common carbide project. The configured .mmp files will create two executable files: YourProject.exe and the SymbianOsUnit.exe file (the names of the executables files are defined in each .mmp file).
Now, just run the symbianosunit.exe file as shown below:
Figure 6: Running your test suite application.
The emulator will run the symbianosunit application which looks like this:
Figure 7: Application running in the emulator.
And this is your first symbianosunit test suite!
Create problematic case
To see how the unit test case can fail, you just need to change the statement in the TestSource.cpp as shown below:
void CTest::testFirstMethod()
{
TInt i = 1; // Value has been changed, so that we can see the failure test result
TInt j = iMyClass->MethodToTest();
TS_ASSERT_EQUALS(i,j);
}
Example source code
- The modified application can be found here: MyFirstUnitTestApp.zip
External links
Portuguese Version
Criando um projeto Symbian C++ com suporte a testes de unidade









Featured article, February 8th 2009 (week 7)
11 Sep
2009
11 Sep
2009