This topic explains how to use C++test to execute automatically-generated and/or user-defined C++test or legacy CppUnit test cases.
Sections include:
C++test can run and report coverage information for any valid C++test or legacy CppUnit test case.
Handling of GCC -nostdinc option If the GCC
|
The general procedure for running test case execution is:
Start the test using the built-in "Run Unit Tests" Test Configuration, or a custom Test Configuration that is based on it.
Tip - Executing Tests from the Test Case Explorer You can execute tests directly from the Test Case Explorer (which can be opened by choosing Parasoft> Show View> Test Case Explorer). Just select the Test Case Explorer node(s) for resource(s) you want to test (projects, folders, test suites, or test cases), right-click the selection, then choose the desired test execution Test Configuration from the Test History or Test Using shortcut menu. Executed tests will be color-coded to indicate their results status. Failed tests will be marked in red. Passed tests will be marked in green. |
Testing Template Functions C++test performs unit testing of instantiated function templates and instantiated members of class templates. See Support for Template Functions for details. |
To generate a detailed coverage report, ensure that the Generate detailed coverage report for [coverage metric] option is enabled in the selected test configuration BEFORE running the test case. Open your test configuration, go to Execution> Runtime and select the option. |
Regularly-schedule batch-mode regression tests should simply execute the built-in "Run Unit Tests" Test Configuration, or a custom Test Configuration that is based on it.
For example:
cpptestcli -data /path/to/workspace -resource "ProjectToTest" -config team://ExecuteTests -publish
C++test can perform a trial build of the test executable, which includes test cases and user stubs, without executing the tests. This feature can be used to check whether auto-generated stubs and tests are can be compiled. You can perform a trial build even if there are not yet any test cases in the tested project.
The recommended way to perform a trial build is to run the built-in "Build Test Executable" Test Configuration.
When you run the built-in "Generate Stubs" Test Configuration (or a custom Test Configuration that is based on it), C++test will automatically generate customizable stubs (or stub templates) for missing function and variable definitions. As described in Executing Test Cases, we recommend that you first generate test cases, then run the Generate Stubs Test Configuration, then run the Build Test Executable Test Configuration before you perform test case execution.
When a test is run using a Test Configuration set to generate stubs, C++test will create a stub file in the specified location. If C++test cannot automatically generate a complete stub definition, it will create a stub template that you can customize (by entering the appropriate return statement, adding include directives etc.). Stub templates will be saved in the stub file before complete stubs.
Automatically generated stubs will be used only if no other definition (user stub or original) is available.
Automatically-generated stubs and stub templates can be customized as described in Adding and Modifying Stubs. If you customize the stubs or stub templates, you need rerun the analysis to prompt C++test to use them.
To create a custom stub generation Test Configuration:
By default, C++test computes the list of the tested files (project files to be tested) and test suites in the following way:
If you want C++test to use any additional project source files to resolve the original definitions from the tested files, you can specify this in the Test Configuration’s Use symbols from additional project files option (available in the Execution> Symbols tab). Additionally, you can use the Use extra symbols from files found in option to specify which stubs are used, and use the Create separate test executable for each tested context option to specify whether you want C++test to create a separate test executable for each context (a single source/header file or a project).
See Symbols tab for more details on how to specify the additional project files and stubs that you want to use, and how you want the test executable prepared.
In certain situations, test policies require that unit tests be applied to code in isolation of the other related components. C++test allows you to perform such testing through the use of stubs.
To test a single file in isolation (on a different “test bed”, including custom and automatic stubs):
When testing a single file in isolation, you might want to use the object and library files filter to prevent testing of specified libraries and objects. In the Test Configuration’s Ignore object/library files field (in the Execution> Symbols tab), you can specify a semicolon-separated list of patterns of command line options. Only options from the linker command line are ignored. Standard compiler libraries and libraries included with pragmas are not filtered.
You can test functions in isolation in many ways. This section describes the approach used frequently for testing safety-critical source code. A fundamental requirement for this kind of testing is that the tested function source code should not be modified by the instrumentation module. This is to ensure that test results reflect function execution in a real production environment. This is to ensure that test results reflect function execution in a real production environment.
To ensure that a tested function is not instrumented, you should verify the instrumentation settings in the test configuration:
Only the following features should be enabled in the Tested sources category:
The following sections will provide additional details on configuring the stubbing mode, managing stubs per test suite configuration, and test executable configuration for testing functions in isolation.
You must enable stubs instrumentation and select the stubbing mode to use function stubs for tested code. Enabling stubs instrumentation activates the stubbing engine. Selecting a stubbing mode defines the way in which stub calls are applied. There are two stubbing modes available:
The following examples illustrate how these modes differ in the way the stub function or method is invoked.
/* Original definition of function to be stubbed */ int Func(void) { // Function body } /* tested function definition */ int testedFunction(void) { int val = Func (); /* tested function body */ return val; } |
The following tables shows the instrumented version of the example source code for both stubbing modes:
Instrument Function Calls | Instrumented Stubbed Functions | ||
---|---|---|---|
|
|
The Instrument stubbed functions mode is usually preferred when testing safety-critical code because it does not change the body of tested function. To set the stubs mode:
Configuring the instrumentation settings in this way introduces some limitations:
The following sections describe how to work with these limitations.
Unit testing strategies sometimes require organizations to use separate sets of stubs for each test suite. This can be accomplished with either a dedicated test configuration for each test suite, or, preferably, by specifying desired stub file(s) directly in the test suite.
To specify stub files for the test suite:
Test suite-specific stub files can also be added to the test suite without using the GUI by modifying the test suite file directly. To add a stub file to the test suite, open the test suite in a text/code editor and insert the following macro for each stub file. You can also use a root directory path to include all stubs from a given location.
CPPTEST_ADDITIONAL_STUB_FILES(<stub file path or root directory>); |
The macro can be specified in any valid C/C++ macro location, but it must be added after the cpptest.h header file. All stub files specified at the test suite level are appended to the stub files affected by the test configuration settings.
To execute a user-defined set of test cases:
Select the related project treenode(s). (You can select and execute test cases from different test suites.)
CDT 4.x Note Test functions are not available in the project tree for Managed C/C++ projects created with CDT 4.x. To execute individual test cases, select the test case name in the code editor. |
C++test may report linker errors if the code under test references symbols from additional files, but C++test cannot find those symbols
To see which symbols are unresolved:
C++test will report unresolved symbols (undefined functions) before the compilation and linking stage. To see the unresolved symbols, do one of the following:
There are several ways to resolve symbols:
If the symbols are in an external library, add the library to the linker command line, then rerun the test.See Updating the Project for details.
See Using a Debugger During Test Execution.