This topic explains how you can define and use factory functions that return specific values of a given type. This allows you to establish repositories of valid objects that can be used in automatically-generated tests and in test cases created with the Test Case Wizard.
Sections include:
A factory function is a function (global, namespace level or a static class method) that returns a given type and has a CppTest_Factory_ name prefix. There can be more than one factory function defined for a given type.
When C++test sees a declaration of such a factory function in the compilation unit for which it generates test cases, it considers this factory function to be one of the possible initialization options for a given type. If the factory function has a non-empty parameters list, C++test will initialize factory function arguments just as it would for arguments of a constructor used to create an object of a given type.
To create a factory function for the type Type, write the function with the following signature:
Type CppTest_Factory_<factory_function_name>(<factory_arguments>); |
For example:
int CppTest_Factory_generateBooleanInt(bool b) { return b ? 1 : 0; } MyClass* CppTest_Factory_createMyClass(int size, int value) { MyClass* m = new MyClass; m->initialize(size); m->setValue(value); return m; } |
Since C++test needs to see the declarations of the factory functions in the context of the compilation unit for which it generates test cases, we recommend the following approach to writing factory functions:
You can quickly add such an include directive to the tested source file by going to the source code editor, typing ffi
, then pressing CTRL+SPACE.
${cpptest:cfg_dir}/safes-tubs;${project_loc}/stubs;${project_loc}/factory
by default.You can quickly add a factory function section to the factory functions source file by going to the source code editor, typing ffs
in the source code, then pressing CTRL+SPACE.
You can quickly add an additional factory function template to the code by going to the source code editor, typing ff
in the source code, then pressing CTRL+SPACE.
|
To configure C++test to use factory functions in automatically-generated test cases:
Factory functions found in the tested compilation unit can also be used when creating test case with the Test Case Wizard. In this case, factory functions will appear as additional initialization methods for a given type.
Any data source that you configure for use in C++test (as described in Using Data From Data Sources to Parameterize Test Cases) can be used in factory functions. You can use the C++test Data Source API to access values from a data source.
Here is an example:
MyClass CppTest_Factory_getMyClassObjectFromDS (void) { MyClass obj; if (CPPTEST_DS_HAS_COLUMN("MyClass.int")) { obj.initialize(CPPTEST_DS_GET_INTEGER("MyClass.int")); } else { // Data Source not available in this test case obj.initialize(0); } |