In this section:
By default, C++test uses predefined strategies for initializing objects when generating test cases—e.g., characteristic values for integer types (0, 1, max, min, etc.), using constructors for class types, etc. By defining factory functions, you can provide additional initializers for types to be used in automatically-generated test cases and test cases created using the test case wizard.
- Open the ATM/ATM.cxx file in the editor.
At the end of the source code,type
ffsand press Ctrl+Space and add the following factory function.
To add another factory function template,type
ffand press Ctrl+Space.
Create a "Generate Unit Tests" Test Configuration
- Choose Parasoft> Test Configurations to open the Test Configuration dialog.
- Select Built-in> Unit Testing and right-click Generate Unit Tests.
- Choose Duplicate from the shortcut menu to copy to the User-defined category.
- Open the Generation> General tab on the new user-defined configuration.
- For Generate tests for function access level, enable protected and private.
- Open the Generation> Test Case tab.
- Enable Use factory functions > Do not use other initializers for types with factory functions.
- Rename the new user-defined configuration to
Generate Unit Tests - Factory Functions.
- Apply the changes made and close the Test Configurations window.
Run the Test Generation Configuration
- In the project tree, select ATM.cxx as the test scope.
- Choose Parasoft> Test Using> User-Defined> Generate Unit Tests - Factory Functions.
- Review the results. Note that some new test cases were generated for private methods of the ATM class.
- In the Test Case Explorer, open tests> autogenerated> TestSuite_ATM_cxx . . . and double-click the test_makeDeposit_1 test case.
- Note that the Bank* and BaseDisplay* arguments of the ATM's constructor are initialized using the factory functions.