![]() |
Home Tutorial |
Friday, 04 July 2008 |
![]() |
|
Unit testing sequenceThe flow chart shown below outlines a typical development cycle when using Symbian OS unit.
Setting up the test frameworkIn order to use Symbian OS Unit to test our CStack class we need to include the dummy application paths in our mmp file. Navigate to the SymbianOsUnitApp\S60\Group folder and open the mmp file. Add the following lines to the file beneath all of the other SOURCE directives. USERINCLUDE ..\..\..\..\Tutorial\DummyProj\inc SOURCEPATH ..\..\..\..\Tutorial\DummyProj\src SOURCE StackClass.cpp Our stack class will now be visible to the Symbian OS Unit project. Create our build files (bldmake bldfiles) and our vc++ project files (abld makefile vc6) and then open the project in vc++. You could try building the project at this point but you will get errors as we have not yet written any tests or built the test runner. You will see in the file browser at framework application and UI files and also a file called TestSource.cpp and its accompanying header file Testheader.h. This is the empty test suite file that accompanies Symbian OS Unit and where we will begin writing our tests.
TestSource.cpp Before we write our first test we need to add our CStack header file to the test suite. So add the line #include “StackClass.h” to the TestHeader.h file. We will describe the suites and the teardown and setup functions later in the tutorial. For now, lets start writing our CStack unit tests. Writing our first testLets begin by writing a test to check that our stack object, once created, has an initial size of zero. We add our tests to a collection of tests called a suite. Test suites must be derived from CxxTest::TestSuite. A single empty test suite called CTest is already included in SymbianOS Unit. All of our tests functions must begin with the word test (lower case) in order for them to be included in the testing framework. Any functions declared within the test suite class that don’t begin with the word “test” are excluded from the tests when we run our test suite. Let create a new test in our test suite called testAfterCreationL. We will use the test to verify that our stack object gets initialised correctly after creation. Add the following lines to our test suite class definition in TestHeader.h:-
and the following lines to the test suite class code in TestSource.cpp :-
The test we have just created firstly creates a new instance of CStack, tests that after creation the current size is zero and then deletes the object. Symbian OS Unit Asset Macro’sWe always within a test suite assert macro. There are currently eight different types of assert available. The assert we have just implemented allows us to check that the two parameters within the assert evaluate to the same value. If they don’t then the test will fail.
Generate the test runnerThe test runner is code generated by a perl script that takes our test declarations and generates a header file called TestDriver.h. This is then used by the framework to execute our tests. Whenever we update our test framework by adding a new test or test suite we have to rebuild the test runner code by running the testgen.bat batch file located in the SymbianOSUnitApp\inc folder. Symbian OS Unit is slightly easier to use than some other C++ test framework alternatives since you don't need to "register" your tests. Running the testgen.bat executes a perl script which generates all of the actual tests for you and automatically registers your tests within the framework. As we have added a new test to our test suite we must rebuild the test runner code now so execute the testgen.bat batch file. Building the test applicationYou can now build the test appication within the ide and run the test application on the emulator.
Now let’s add another couple of tests to the suite that check that we can push an entry onto and pop an entry from the stack. Let’s also check that an attempt to pop an entry from an empty stack generates an exception.
Add the following code to the suite. (Don’t forget to add the function declarations to the class definition).
Save the changes and run the testgen.bat batch file to update our testrunner code. Now run the tests. On examination of the CStack pop function we can see that we don’t trap for pop being called on an empty stack so lets update our code. We will add an assert that will trap for popping an empty stack and leave with the KErrUnderflow error code. (Note: we will now be changing the pop function to a leaving function so don’t forget to append an L to function name and change the test suite references to popL)
Test FixturesYou will probably have noticed that we created and then destroyed a CStack object in each of the tests. This is time consuming and inefficient so we could employ a fixture to do the work for us. This is where we use the setup() and teardown() functions that you may have noticed declared in our test suite class. They allow us to define the common code that we want to run before and after every test function. In our example we could move the creation of and destruction of the CStack object to the test fixtures and create a single CStack *iStack object in the class declaration.
Now when we add a new test we know that a newly created CStack object will automatically be created ready for us to test. Test suitesSo far we have only used one test suite in our test build. What if we wanted to write a number of tests that are very time consuming and therefore don’t want to run them every time we test? One solution is to put them in a different test suite. We are then able to just select the other test suite when we want to run the fast test during development maybe and only run the slow test on final build. Lets create a test that pushes a number onto then off the stack a large number of times and put it in a new suite. You can create a new suite in its own file but we will just add it to our example file. Lets call our suite CTestSlow to indicate that our tests in this suite are time consuming tests. AS previously stated a test suite must be derived from CxxTest::TestSuite Add the following code to the testsource.cpp file:-
Add the new test suite class definition to the testheader.h file:-
Don’t forget to run testgen.bat to rebuild the testrunner before building the application for testing. If you add a new test suite to your project (e.g. TestNewSuite.ccp,.h) in its own code and header file then you must update the test runner build file testgen.bat to include the new suite header filename:-
and also the ExtraTestBuildTasks.bldmake file:-
Memory leak checkingSymbian OS unit implicitly checks for memory leaks for each test. If a leak is detected at the end of a test then the test will fail. To demonstrate this functionality update the CStack constructor by adding an allocation is shown below.
Logging
A log file containing a similar output to the text output on the application dialog is created automatically by Symbian OS. The file is named SOSUnit.log and is created in the “c:\logs\” folder of the emulator or target hardware. If this folder doesn’t exist then the log wont be created. The contents of the log file is shown below for the memory leak tests we have just run above.
UIQ, UIQ3 and Series 80Although we have used the series 60 3rd Edition platform to demonstrate Symbian OS Unit’s functionality in this tutorial, it also supports the UIQ, UIQ3 and Series 80 reference platform. It works in exactly the same way and with an almost identical UI as shown below.
UIQ Series 60
Series 80
Important Note:
Testheader.h |
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|