Stubbing overview

Stubs are simulations of actual functions, which can be used to isolate the function under test and to check that calls to the stubbed function are correctly formulated.

Stub simulation is based on the idea that certain functions are simulated and are replaced with stubs generated in the test harness. Stubs provide the same interface as the simulated functions, but the body of the functions is replaced with a basic behavior. From the point of view of other functions in the test harness, the stub looks identical to the actual function that it simulates.

Stubs can be used in the following roles:
  • Retrieving and storing input values to stubbed functions from a function under test.
  • Assigning output values from the stubbed functions to a function under test.
Stubs are described with the following elements:
  • A variable array for the input parameters of the stub.
  • A variable array for the output parameters of the stub.
  • A body declaration for the stub behavior.

To create a stub, the source code of the stubbed function must be included in the project. HCL OneTest™ Embedded analysis the prototype of the stubbed function to generate a stub with the same interfaces. Once the stub is created, you use the stub editor to define the stub checks, which verify that each parameter in the call to the stubbed function matches an expected expression.

Stub checks are based on the sequential number of the call, which typically reflects the iteration of the calling function in the test case. The sequential call number is expressed as a range. For example a stub check for a parameter a can be set to match an expected expression x for the first 10 calls received by the stub (range 0 to 10), an expression y for the 11th to 20th calls (range 10 to 20), and an expression z for any following calls (range Others).

Stub expected value expressions

The expected expressions are used to specify a test criteria by comparison with the value of a call parameter received by a stub. The test receives a passed verdict when the actual obtained value matches the expected value expression.

The expected expressions for a stub can be among any of the following values:
  • Numeric (integer or floating-point), character, or character string literal values. Strings can be delimited by single or double quotes.
  • Native constants, which can be numeric, characters, or character strings.
  • Ranges with lower and upper values and inclusive or exclusive bounds.
  • Global variables that are declared by the program under test.
  • A null pointer or a non-null pointer.
  • Arrays and structures, any of the above-mentioned expressions between braces ('{}').
  • C functions or expressions with one or more of the above elements combined using any operators and casting, with all required levels of parentheses. The + operator allows to concatenate character string variables.
  • No Check, which specifies that no check is performed on that variable.
  • Same As Init, which specifies that the expected variable equals the initialization expression.
  • Data sets that are synchronized with a multiple initialization expression.

The data type of the variable defines the acceptable values for the expected value.

Numeric values can be associated with a comparison operator in the stub editor.

Stub return value

Return values are used for parameters and functions if a return value is defined in the signature of stubbed function. A special line named return in the parameter table is added to define the value for the return value of the function.

A return value can be defined for ouput parameters or input/output parameters. Change this setting in the Mode column. The return value is a C native expression as numeric, character, or string...

The function's return value can be replaced by a special user source code. In this case, write the appropriate C source code and add the return statement so that the function returns a value to the calling expression. To activate this feature, select the return line and click the Use source code rather than return type tool button. The user source code panel is activated and the C source code can be added.

Stub memory usage

For each STUB, the test harness allocates memory for the following tasks:
  • Storing the expected expression of the input parameters during the test.
  • Storing the obtained value of the input parameters during the test when an error is detected.
  • Storing the values assigned to output parameters before the test.
A stub can be called several times during the execution of a test. The test harness allocates memory for expected and returned values in accordance to the maximum number of calls to the stub in the test harness.

You can reduce the stub memory allocation value to a lower value in the configuration settings when running tests on a target platform that is short on memory resources.

Feedback