Overview
With Unit Test Assistant you can easily create test suites by choosing the Create test suite option in the Unit Test Assistant view or in your IDE context menu. When you choose the Create test suite option, UTA will:
- Analyze the methods in the selected scope.
- Create test cases.
- Optional: Use AI to improve failing tests generated by UTA. (UTA may create a limited number of failing tests that provide some coverage even though they fail. In this step, UTA uses AI to update the failing tests to make them pass.)
To use this functionality:
- The LLM provider must first be configured in the Parasoft Preferences (see Configuring LLM Provider Settings).
- The Use AI to fix failing generated tests (requires LLM provider to be configured) option must be enabled in the UTA Preferences (see Test creation options).
This feature depends on LLMs, which may produce inaccurate information. Run the created tests using the Track and make assertions functionality to add assertions (see Tracking Object Changes and Creating Assertions for details).
If you are using Eclipse IDE, the Build Automatically option (Project menu> Build Automatically) must be enabled for assertions to be added. If the option is disabled, no assertions are added during the test run.
To customize test cases created by UTA, review the values and assertions in the test code and update them as necessary. In addition, you can re-run the tests with the Run option to enhance your tests cases using UTA recommendations (see Executing Unit Tests with Unit Test Assistant).
Creating a Test Suite for One File
Open the Unit Test Assistant - Create test suite dialog in one of the following ways:
Select a class in the editor and click the Create test suite action link in the Unit Test Assistant view:
Right-click a file in Project Explorer and select Unit Testing> Create test suite from the context menu:
Configure the test creation options.
Optimize test suite for: - Allows you to specify your priorities for creating tests for the selected methods.
- Maximum code coverage - UTA will create as many tests as is required to cover the maximum possible number of method execution paths. Enabling this option will help you achieve the highest coverage rate, but the complexity and potential overlap of test cases may impact their maintenance.
- Test suite maintainability - UTA will create fewer tests only to cover the main execution paths. Enabling this option will help you create less complex and better maintainable tests, but the coverage rate will be lower.Mock initialization - Allows you to specify how you want mocks to be initialized.
- Configure within test method - Initializes mocks by declaring and initializing the object under test and its dependencies within the test method.
- Use @InjectMocks - Initializes mocks by using the @InjectMocks annotation to mark fields on which mock injection should be performed and the @Mock annotation for dependencies. See Configuring Mock Initialization for details.- Test template - allows you to specify the template used for creating new classes. See Configuring Test Templates for details.
Create test cases for: - Allows you to specify the methods for which you want to create test cases. You can use Select All, Deselect All, or Select Modified (*) buttons to update the selection in the list. The Select Modified (*) button selects only the methods which have been modified in source control (indicated by an asterisk).
Note: Eclipse and IntelliJ must recognize the project as a Git project in order for the modified methods functionality to work properly
- Click OK. UTA will:
- create test cases for all the selected methods in the file (including methods that already have corresponding tests).
- run the tests to create assertions.
- Review the tests to modify the values and assertions if necessary.
- Run the test with UTA to collect coverage information and recommendations for possible improvements (see Executing Unit Tests with Unit Test Assistant).
Creating a Test Suite for Multiple Files
UTA can create test cases for more than one file at a time. You can create tests either for all methods in a set of specified files, or only for the modified methods in a set of files based on the comparison criteria you select.
- Right-click the files, package(s), or project(s) for which you want to create test cases. Also, you can select individual methods if they are in the same class (if you select methods from different classes, or an individual method and a class or package, test cases will not be created).
Click Unit Testing> Create test suite to open the the Unit Test Assistant -Create test suite dialog.
- Configure the test creation options.
- Optimize test suite for: - Allows you to specify your priorities for creating tests for the selected methods.
- Maximum code coverage - UTA will create as many tests as is required to cover the maximum possible number of method execution paths. Enabling this option will help you achieve the highest coverage rate, but the complexity and potential overlap of test cases may impact their maintenance.
- Test suite maintainability - UTA will create fewer tests only to cover the main execution paths. Enabling this option will help you create less complex and better maintainable tests, but the coverage rate will be lower. Mock initialization - Allows you to specify how you want mocks to be initialized.
- Configure within test method - Initializes mocks by declaring and initializing the object under test and its dependencies within the test method.
- Use @InjectMocks - Initializes mocks by using the @InjectMocks annotation to mark fields on which mock injection should be performed and the @Mock annotation for dependencies. See Configuring Mock Initialization for details.- Test template - Allows you to specify the template used for creating new classes. See Configuring Test Templates for details.
- Create test cases for - Allows you to specify the methods for which you want to create test cases.
- Entire selection - Test cases will be created for all source files in the selection.
- Locally modified methods - Test cases will be created only for the methods in the selection which have been modified.- When compared to - Tests will be created for all modified methods found when comparing the files on disk to their contents in the provided commit.
- Since current branch diverged from - Tests will be created for all methods modified within the project's current branch since the branch diverged from a specified branch. The branch from which the current branch was created must be specified.
- Commit or branch - Allows you to specify a commit, branch, or tag used for the When compared to and Since current branch diverged from settings. The commit or branch dropdown menu stores the input history. It includes two preset values for the When compared to setting:
- "Latest commit on local branch" - Tests will be created for locally modified, un-committed code.
- "Latest commit on remote branch" - Tests will be created for locally modified, un-committed code as well as changes in any local commits that have not been pushed to a remote branch.
Note:
Git reflog references are not supported.
Consider the following recommendations for using the Locally modified methods and Commit or branch settings:
If you have local changes only and want to generate tests for the impacted code pre-commit, we recommend using the "When compared to" and "Latest commit on local branch" settings.
If you have local commits that have not been pushed and you want to generate tests for those commits, including any current local changes, we recommend using the "When compared to" and "Latest commit on remote branch" settings.
If you have pushed commits to a branch, such as a feature or development branch, and now that the feature or fix is complete, you want to generate tests for all the changes made in the branch, we recommend using "Since current branch diverged from" and specifying the parent branch of the current one. For example, if you are using a feature branch "feature/foo" which was branched from master, you could specify "master".
- The Locally modified methods and Commit or branch settings are not available when a Git repository is not configured for the selected code.
- Optimize test suite for: - Allows you to specify your priorities for creating tests for the selected methods.
- (Optional) Hover over the License usage summary option to display an estimated number of tokens required to create test cases for the selected resources. Once the test are generated, the summary dialog will show how many tokens were actually consumed, and how many remain (see Licensing).
- Click OK. UTA will:
- create test cases, skipping the files that are not testable or whose corresponding test classes already exist.
- run the tests to create assertions.
- Run the tests with UTA to collect coverage information and recommendations for possible improvements (see Executing Unit Tests with Unit Test Assistant).
Licensing
Creating tests for modified methods when compared to a local or remote branch requires the "Unit Test Single Class Creation" license feature, and no license tokens are consumed in this scenario. Creating tests for an entire selection, or for modified methods when compared to a specified commit or branch or since the current branch diverged from a specified commit or branch, requires one of the "Unit Test Bulk Creation Tier" license features. These features provide license tokens that are consumed based on the number of files for which test cases are generated (the number of tokens available depends on your license agreement). In these scenarios, UTA consumes one token per .java file for which tests are created.
- Files for which UTA does not generate any test cases (for example, interfaces or abstract classes) do not consume a token.
- Once a token has been consumed for a given java file, regenerating tests for that .java file does not consume another token, even if the first test is deleted.
Potential setup problems
If the results of bulk creation are poor, this might indicate setup problems in the project. For more information, see How can I diagnose potential setup problems?.