Using a code coverage tool

You can use code coverage tools to find out how well the tests cover the code being tested. BullseyeCoverage is commonly used in Symbian development. The process is as follows:

  1. In BullseyeCoverage, turn on the coverage compiler by pressing the button shown in the figure below.

  2. Recompile the tests.

  3. Run the tests in the emulator.

  4. View coverage results.

Figure: Code coverage after test round

The results include function coverage and branch coverage. From Figure 1, it can be seen that SendSmsL() was fully tested (from a structural point of view) and ParseMsgUid() partially. In the detailed coverage view (see Figure 2), the source code is shown and branches are marked with a description if not all decision paths were executed.

Figure: Detailed coverage analysis

Raising the branch coverage of a method to 100 percent can be a difficult task. A common advice in unit testing is to keep test cases small and simple. This is why many test cases are needed until the target method's coverage is raised to an acceptable level. Test cases should pass the expected values for the method under test until all interesting branches in the target are walked through during execution. You may also need to modify the target object's state outside before calling the actual implementation from the test. You can achieve this by

  • Altering attributes directly, when those are public;

  • Altering attributes indirectly by calling necessary methods which change the state (for example, when there are setter methods for the needed attributes).

If target class attributes are protected or private, you need to adopt a special approach, such as

  • Define a test class as a friend for a test target , for example:

    class CMapExampleSmsEngine :    public CBase,
                                    public MMsvSessionObserver
    		{
    #ifdef __SYMBIANOSUNIT
    		friend class CMapExampleSmsEngineTest;
    #endif
    		...
    		}
    
  • Derive a wrapper class from the test target class and then implement methods to the wrapper, which give access to protected attributes in the actual class;

  • Use precompiler directions in the class under test to provide more open access to class attributes or even in the class implementation. Note that the code split to different blocks guarded by precompiler directives makes code less readable and may even raise new bugs that are hard to figure out.