...
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:
- Click the Auto Generate button in the Test Case Wizard's editor page, click the Auto Generate button.
- 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
- Parameterizing Test Cases in the Test Case Editor
- Parameterizing Test Cases with the Test Case Wizard
Anchor | ||||
---|---|---|---|---|
|
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.
- Open the Test Case Explorer and right-click on a test suite
- Choose Add New> Test Case using Editor…
- Choose a function from the Create test case for drop-down list.
- Click the Create new parameterized test case button () to open the data source selection dialog.
- 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.
- Save the test case to add it to the test suite.
Anchor | ||
---|---|---|
|
...
|
...
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 | ||
---|---|---|
| ||
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:
- In the Test Case Explorer, right-click the test suite node, then choose Add New> Test Caseusing Wizard from the shortcut menu.
- 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.
- Click Next to open the next wizard page.
- Check the Use data source check box and choose the appropriate data source.
- 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}".
- 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:
- Specify which data source to use with the registration macro
CPPTEST_TEST_DS(<TEST_CASE_NAME>,
CPPTEST_DS("<MANAGED_DATA_SOURCE_NAME>")); - 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 | ||||
---|---|---|---|---|
|
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 |
| N/A |
Return a char value |
| N/A |
Return an integer value |
| N/A |
Return an unsigned integer value | unsigned long long CPPTEST_DS_GET_UINTEGER(c onst const char* NAME) | N/A |
Return a floating point value | long double CPPTEST_DS_GET_FLOAT(cons t const char* NAME) | N/A |
Return a boolean value | int CPPTEST_DS_GET_BOOL(const char* NAME) | N/A |
Return the memory buffer | const 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 thereint* SIZE_PTR) | If SIZE_PTR is not null, the size of the buffer will be stored there. |
Return an enum value |
| <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 array | CPPTEST_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 type | C++test API function | Supported format |
---|---|---|
bool | CPPTEST_DS_GET_BOOLEAN() | For true:
For false:
|
signed integer | CPPTEST_DS_GET_INTEGER() | Decimal representation of the integer (optionally prefixed with - or + sign); e.g.:
Hexadecimal representation of the integer prefixed with 0x or 0X (optionally prefixed with - or + sign); e.g.:
Octal representation of the integer prefixed with 0 (optionally prefixed with - or + sign); e.g.:
Binary representation of the integer prefixed with 0b or 0B, (optionally prefixed with - or + sign); e.g.:
|
unsigned integer | CPPTEST_DS_GET_UINTEGER() | Decimal representation of the integer; e.g.:
Hexadecimal representation of the integer prefixed with 0x or 0X; e.g.:
Octal representation of the integer prefixed with 0; e.g.:
Binary representation of the integer prefixed with 0b or 0B; e.g.:
|
floating point | CPPTEST_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 / 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:
|
char | CPPTEST_DS_GET_CHAR() | Character to be used; e.g.:
Additionally, the following C-like escape sequences should be used to handle special characters:
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.:
|
string | CPPTEST_DS_GET_CSTR() | Character string to be used; e.g.:
Additionally, the following C-like escape sequences should be used to handle special characters:
Example:
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.:
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.:
Note that this can be used with the UI-managed data sources only (test cases registered with CPPTEST_DS("ds_name") macro). |
data buffer | CPPTEST_DS_GET_MEM_BUFFER() | Use the same format as for the string type (see above). |
...
as for the string type (see above). | ||||
enum value | CPPTEST_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.
|
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); } |
...