The Autogenerated C++ Component Test

Once you become familiar with the layout of the autogenerated test and contract check, the modifications you need to make to increase code coverage will become obvious.

To complete the test script:

  1. In the Project Browser tab on the right-hand side of the screen, double-click the node PhoneNumber.otd.

  2. Maximize the test script editor.

This is the test driver script. In it you will perform those steps necessary to drive and test classes in the file under test.

Along with the .otc contract-checking test - discussed in the next section - full C++ class testing is possible. The idea is that the files PhoneNumber.cpp, PhoneNumber.otd, and PhoneNumber.otc will be compiled and executed together (with execution taking place on the target specified by the currently selected Target Deployment Port Configuration).

C++ component testing test scripts are written with a compiler-independent test script API. For detailed information about the script layout, take advantage of the OneTest Embedded Reference Guide. For the Tutorial, only critical script elements will be discussed.

Each class used in the file under test is assigned its own TEST CLASS block - PhoneNumber.cpp only handles the PhoneNumber class, so there is only one Test Class block. Each TEST CLASS block is divided into a single PROLOGUE , one or more TEST CASE blocks, and then a single EPILOGUE.

The PROLOGUE statement defines native code that is to be executed whenever the surrounding TEST CLASS execution begins. You typically use the PROLOGUE statement to declare and sometimes initialize the object instances of a class under test. In this exercise, the generated PROLOGUE creates an instance of the class PhoneNumber. The EPILOGUE structure defines native code that is to be executed whenever the execution of the surrounding TEST CLASS ends.

The TEST CASE block generates a public method test of the class under test. The test case name is made up of the identifier of the method under test with the prefix test. This ensures correct overload handling.

A typical test starts with the display of a trace (with the PRINT statement) and continues with the C++ native code that calls the method under test. This call is performed on the instance declared in the PROLOGUE block. Any parameter values are null. If the method under test returns a value, the test case continues with a CHECK statement. The test case ends with another trace display.

For this tutorial, we would like to call the PhoneNumber constructor with an integer value of 0. Since your goal is to simply increase code coverage, don't bother testing anything - just call the PhoneNumber constructor with a value of 0.

  1. In the PROLOGUE block, add (0) after the obj0 identifier, so that it appears as follows:

PROLOGUE

{

// Declarations of variables needed by this test class.

// Actions to be performed before executing this test

// class.

#PhoneNumber obj0 (0) ;

}

 

The # symbol indicates that the line contains native C++ code.

  1. From the File menu, select Save.

Technically, you are finished. When this test script is executed, the PhoneNumber constructor will be called with an integer value of 0. However, to give you some idea of how an assertion test would be useful, the next topic will take a look at the contract checking script.