IBM Support

Component Testing C: How to test a static variable of a function

Question & Answer


Your C code under test contains a static variable. How do you access the variable within the IBM® Rational® Test RealTime™ test script?


The C language does not allow visibility of this variable outside the function, in which you declared the variable.


Modify your code under test, if you really need access to this variable. The reason is that you can not test from within the test script.

If modifying the code under test is not an option, you need to plan your test script more carefully. You might need several test scripts or test executions, because you can not reset this variable.


For example, the code under test contains the following function.

void f1 (int input)


static int invisible = 0;

/* any code here */


can you test the variable invisible?

The only possibility is to make the variable visible outside the function during testing.
If you can change the code under test, you can introduce a macro, for example _TESTRT_TEST. When you define _TESTRT_TEST, the variable is visible to the test script. You need to define this macro when you generate the test script as well as during the compilation of the code under test.

You can use the macro as follows: 

void f1 (int input)

extern int invisible;
 static int invisible = 0;

/* any code here */


Then you define the macro when you compile the code under test. When you use the Test RealTime user interface, you can do it in the Settings dialog box.

Click Project > Settings.
  1. In the Settings dialog box put _TESTRT_TEST in the section:
    Build > Compiler > Preprocessor macro definitions.
  2. Declare the variable in the test script, .ptu, as a global variable global. For example:

#int invisible;

and test like this:

VAR invisible, init = 0, ev = 2

Why is it impossible to test the variable without modifying the code under test?

Outside of Test RealTime, how would you test this variable? How would you modify it from another function?

The C language does not allow you to access a static variable outside of the function in which the variable is declared. That is the whole point of making the variable static to the function in the first place.

Therefore, if you do not modify your code under test, you need to find a sequence of tests. With this sequence you can pass by all possible states of this static variable. You modify the external interfaces to the function, its parameters and the global variables that it uses.

Which sequence to choose depends upon the code in the function under test. Therefore the answer is outside the scope of this technote.


All source code and/or binaries attached to this document are referred to here as "the Program". IBM is not providing program services of any kind for the Program. IBM is providing the Program on an "AS IS" basis without warranty of any kind. IBM WILL NOT BE LIABLE FOR ANY ACTUAL, DIRECT, SPECIAL, INCIDENTAL, OR INDIRECT DAMAGES OR FOR ANY ECONOMIC CONSEQUENTIAL DAMAGES (INCLUDING LOST PROFITS OR SAVINGS), EVEN IF IBM, OR ITS RESELLER, HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.

[{"Product":{"code":"SSSHUF","label":"Rational Test RealTime"},"Business Unit":{"code":"BU053","label":"Cloud & Data Platform"},"Component":"Component Testing: C","Platform":[{"code":"PF025","label":"Platform Independent"}],"Version":"2003.06.15;7.0;;7.0.5;","Edition":"","Line of Business":{"code":"LOB45","label":"Automation"}}]

Historical Number


Document Information

Modified date:
16 June 2018