Page tree

Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Published by Scroll Versions from space CPPTDESKDEV and version 10.4.2

...

Adding Existing Data Sources at the Workspace Level

...

...

To create a managed data source at the workspace level:

...

C++test can generate a comma separated value file (.csv) or a C++test managed table data source with automatically-generated data source values.

...

For information about automatically generating new data sources in the Test Case Editor, see Using Data Sources to Parameterize Test Cases Added in the Test Case Editor.

If are configuring a test case in the Test Case Wizard:

  1. Click the Auto Generate button in the Test Case Wizard's editor page, click the Auto Generate button.
  2. Specify the desired settings in the Auto Generate window that opens.


...

Once it is generated, the generated data source can be used to parameterize other test cases in in the test suite.

Using Data Source Values to Parameterize Test Cases

Anchor
Using Data Sources in the Test Case Editor
Using Data Sources in the Test Case Editor
Using Data Sources to Parameterize Test Cases Added in the Test Case Editor 

You can use data source values to parameterize test cases created with the Test Case Editor (See Adding Test Suites and Test Cases with the Test Case Editor). The values can be provided in a data source table, which is built into the test case, or in an external data source.

  1. Open the Test Case Explorer and right-click on a test suite
  2. Choose Add New> Test Case using Editor…
  3. Choose a function from the Create test case for drop-down list.
  4. Click the Create new parameterized test case button (Image Added) to open the data source selection dialog.
    Image Added
  5. Select the data source you want to use. The following options are available:
    Built-in table - adds a table for specifying parametrization values to your test case (see Adding Test Case Parameterization for details about Parameters API you can use to access the values specified in the built-in table).
    External data source - automatically generates an external data source for specifying specifying parametrization values : a comma separated value file (.csv) or a table data source If you choose this option, specify the desired settings in the Auto Generate window that opens and click OK. The data source is generated at the test suite level.
    Image Added
         
  6. Save the test case to add it to the test suite.

Anchor
Using Data Source Values to Parameterize Test Cases

...

Using Data Source Values to Parameterize Test Cases
Using Data

...

Sources to Parameterize Test Cases Written in the Test Case Wizard

You can use data source values to parameterize existing (automatically-generated or user-defined) test cases, as well as to define test case values as you write test cases in the Test Case Wizard. Data source values can be taken from any data source that is defined in C++test as described above.

Note
titleNote

C++test cannot automatically verify the unverified outcomes and failed assertions that result from the execution of a test case that was parameterized with data source values. This is because it typically requires using an expected outcome that is also parameterized with the data source.

Configuring Data Source Usage from the Test Case Wizard

To use the Test Case Wizard to add a new test case that pulls values from a previously-defined managed data source:

  1. In the Test Case Explorer, right-click the test suite node, then choose Add New> Test Caseusing Wizard from the shortcut menu.
  2. On the first page, specify the source file (compilation unit) and the function for which you want to add a test case, then enter a name for the test case.
  3. Click Next to open the next wizard page.
  4. Check the Use data source check box and choose the appropriate data source.



  5. Configure the test case by specifying its input and expected output values using GUI controls.
    • To use a data source value for a given pre- or -post-condition, double-click the related node then select the appropriate data source column name. Data source values are listed in the combo-box as the column name surrounded with curly braces—for example, "{myColumnName}".
  6. Click Finish to generate the test case. The new test case will be added to the test suite and the generated source code will be opened in the editor.

Configuring Data Source Usage from the Test Case Code

To parameterize an existing (automatically-generated or user-defined) test case with a previously-defined managed data source:

  1. Specify which data source to use with the registration macro
    CPPTEST_TEST_DS(<TEST_CASE_NAME>,
    CPPTEST_DS("<MANAGED_DATA_SOURCE_NAME>"));
  2. Specify how to use the data source values with the CPPTEST_DS* macros (explained in Macros for Accessing Data Source Values).

Example

For example, to parameterize an existing test case with values from an Excel sheet which we will call "MyDataSourceForSum":

...

When the test case is executed, it will be parameterized with values from the  "MyDataSourceForSum" Data Source of Excel type.

Macros for Accessing Data Source Values
Anchor
Macros for Accessing Data Source Values
Macros for Accessing Data Source Values

The following macros can be used to access values from a data source. Each macro takes the parameter NAME, which specifies a unique identifier for a data source column.

To do this...Use this macro...Notes
Return a null-terminated string value

const char*
CPPTEST_DS_GET_CSTR(const char* NAME)

N/A
Return a char value

char

CPPTEST_DS_GET_CHAR(const char* NAME)

N/A
Return an integer value

long long
CPPTEST_DS_GET_INTEGER(

co nst

const char* NAME)

N/A
Return an unsigned integer valueunsigned long long CPPTEST_DS_GET_UINTEGER(c onst const char* NAME)N/A
Return a floating point valuelong double
CPPTEST_DS_GET_FLOAT(cons t const char* NAME)
N/A
Return a boolean valueint
CPPTEST_DS_GET_BOOL(const char* NAME)
N/A
Return the memory bufferconst char*
CPPTEST_DS_GET_MEM_BUFFER (const char* NAME, unsigned int* SIZE_PTR)If SIZE_PTR is not null, the size of the buffer will be stored there.int* SIZE_PTR)
If SIZE_PTR is not null, the size of the buffer will be stored there.
Return an enum value

<scoped enum name>

CPPTEST_DS_GET_ENUM(<scoped enum name>, const char* NAME)

<scoped enum name> is a full name of enumeration including all namespace names. For example: INNER_NS::MyEnumeration, INNER_NS::MyClass::MyEnumeration 
Return a value from SOURCE arrayCPPTEST_DS_GET_VALUE(SOUR CESOURCE)

A variable of array type should be specified as the SOURCE parameter:

The number of the row from which the values are extracted will be automatically increased after each subsequent test case execution.

Return the current iteration (row number)unsigned int
CPPTEST_DS_GET_ITERATION( )
Can be used to determine the current iteration/row number.
Return a non-zero value if column 'name' exists in the current iteration of the current data source.int
CPPTEST_DS_HAS_COLUMN(const char* name)
Can be used in stubs for test case specific behavior (see Using Data Sources in Stubs ). Zero is returned if data source is not used.

Format of Data Source Values

The following table provides information about the supported formats of the data source values for particular types:

Data source typeC++test API functionSupported format
boolCPPTEST_DS_GET_BOOLEAN()

For true:

    • true
    • non-zero integer constant; e.g. 1

For false:

    • false
    • 0
signed integerCPPTEST_DS_GET_INTEGER()

Decimal representation of the integer (optionally prefixed with - or + sign); e.g.:

    • 0
    • 1
    • -200
    • +55

Hexadecimal representation of the integer prefixed with 0x or 0X (optionally prefixed with - or + sign); e.g.:

    • 0x1a
    • -0XFF
    • +0x12

Octal representation of the integer prefixed with 0 (optionally prefixed with - or + sign); e.g.:

    • 0711
    • -0123
    • +055

Binary representation of the integer prefixed with 0b or 0B, (optionally prefixed with - or + sign); e.g.:

    • 0b01010101
    • -0B1111
    • +0b11
unsigned integerCPPTEST_DS_GET_UINTEGER()

Decimal representation of the integer; e.g.:

    • 0
    • 1
    • 200

Hexadecimal representation of the integer prefixed with 0x or 0X; e.g.:

    • 0x1a
    • 0XFF

Octal representation of the integer prefixed with 0; e.g.:

    • 0711
    • 0123

Binary representation of the integer prefixed with 0b or 0B; e.g.:

    • 0b01010101
    • 0B1111
floating pointCPPTEST_DS_GET_FLOAT()

The actual format of the floating point value depends on the configuration of the compiler used—the actual implementation of the CPPTEST_STR_TO_FLOAT / CPPTEST_SCANF_FLOAT /
CPPTEST_SCANF_FLOAT_FMT macros. In most cases, the  sequence of decimal digits possibly containing a radix character (decimal point, locale dependent, usually ''.'').

Optionally, this can be followed by a decimal exponent. A decimal exponent consists of an ''E'' or ''e'', followed by an optional plus or minus sign, followed by a non-empty sequence of decimal digits, and indicates multiplication by a power of 10.

Examples:

    • 100
    • 1.5
    • -3.14159
    • 0.44E7
    • 99.99e-15
charCPPTEST_DS_GET_CHAR()

Character to be used; e.g.:

    • a
    • 0
    • $

Additionally, the following C-like escape sequences should be used to handle special characters:

    • \n
    • \t
    • \v
    • \b
    • \r
    • \f
    • \a
    • \\
    • \'
    • \"

For non-printable characters, an octal (1-3 digits prefixed with \) or hexadecimal (1-2 digits prefixed with \x) representation of the character can be used; e.g.:

    • \13
    • \017
    • \x0A
stringCPPTEST_DS_GET_CSTR()

Character string to be used; e.g.:

    • abcdefgh
    • qaz123
    • Hello world!

Additionally, the following C-like escape sequences should be used to handle special characters:

    • \n
    • \t
    • \v
    • \b
    • \r
    • \f
    • \a
    • \\
    • \'
    • \''

Example:

    • Hello, \"world\"!

To use non-printable characters in the string, an octal (1-3 digits prefixed with \) or hexadecimal (1-2 digits prefixed with \x) representation of the character can be used; e.g.:

    • \13
    • \017
    • \x0A

To define the null as the C-string value in a data source, a managed data source can be configured to recognize some character; e.g. $, as a Special Value Prefix and then the NULL prefixed with the Prefix character can be used in the data source; e.g.:

    • $NULL

Note that this can be used with the UI-managed data sources only (test cases registered with CPPTEST_DS("ds_name") macro).

data bufferCPPTEST_DS_GET_MEM_BUFFER()Use the same format as for the string type (see above).

...

as for the string type (see above).
enum valueCPPTEST_DS_GET_ENUM()

A simple enumerator name or a full name that includes all namespace names can be used. For example, if the MON enumerator is defined in ::INNER_NS::MyClass::DaysEnumeration, you can type either MON or ::INNER_NS::MyClass::MON.

Info

To use enumerators in Data Sources and Parameters step, enable Test Configuration’s option: “Enable enum data autogeneration”. You can find this option in: “Test Configuration > Execution tab > General tab > Instrumentation mode > Edit > Instrumentation features > Advanced options”.


The following table provides information about  formatting data source values using particular data source types:

...

Database (managed data source)CPPTEST_DS()No database-specific value preparation is needed. Note that the data needs to be formatted to be used for particular types as described above.

Exploring Available Data Sources

Viewing/Modifying a Data Source Configuration

...

  • Right-click the Test Case Explorer node for that data source, then choose Edit from the shortcut menu.

 


You can then view the data source configuration in the window that opens, and modify it as needed.

...

To ensure data source portability, all test suite level data source information is stored in a file with the same name as the test suite file name and a .properties extension. As a result, each test suite has its own data source settings file and can be moved along with these data.

 


Portable data source mode applies only to data sources created at test suite level—it does not apply  to data sources created at the project orworkspacelevel.

...

  • This macro uses ARRAY_NAME as a data source. ARRAY_NAME should be of type const char* data[ROW][COLUMN].
  • The first row in the table must contain columns names.
  • The test case will be executed (ROW - 1) times.
  • The data source access macros can be used to extract values as described in the following sections.

...


With CPPTEST_DS_REPEAT(NUMBER), note that:

...

  • Data source arrays can be declared outside of a test suite class (this is the only option for C test suites) or as a test suite class member (for C++ test suites). Note that C++ test suites can be used for the C language (assuming that you have a C++ compiler to build them).
  • When data source arrays are declared as a class member, scope of access is not important because the arrays are only accessed by test case functions and setup/teardown, which are member functions.
  • Declaring typed arrays and initializing them in setUp method is a very powerful way to initialize data source array elements to arbitrary data. This data can be dynamically allocated objects, factory test objects, etc. For example:

...


Code Block
#include "cpptest.h" 
int plus_one(int);
class TestSuite_3 : public CppTest_TestSuite {
public:
    CPPTEST_TEST_SUITE(TestSuite_3);
    CPPTEST_TEST_DS(test_ds_repeat, CPPTEST_DS_REPEAT(2));     
    CPPTEST_TEST_SUITE_END();
 
    void setUp();
    void tearDown();
 
    void test_ds_repeat();
private:
    int _dsRepeat_arg[2];
    int _dsRepeat_return[2];
};
CPPTEST_TEST_SUITE_REGISTRATION(TestSuite_3); 
 
void TestSuite_3::setUp()
{
    _dsRepeat_arg[0] = 1;
    _dsRepeat_arg[1] = 2;
}
void TestSuite_3::tearDown()
{
}
void TestSuite_3::test_ds_repeat()
{
    int index = CPPTEST_DS_GET_ITERATION() - 1;
    int _value = _dsRepeat_arg[index];
    int _expected = _dsRepeat_return[index];
 
    int _return = plus_one(_value);
    CPPTEST_ASSERT_INTEGER_EQUAL(_expected, _return);
}

...