This documentation applies to the on-premises installation of watsonx Code Assistant on IBM Software Hub. For IBM Cloud documentation, see Generating unit tests.
Generating unit tests
Generate unit tests for various programming languages from the chat, the CodeLens in the editor, or the context menu.
Eclipse IDE only: For best results with a multimodule Java application, ensure that you import the aggregator module and each submodule as separate projects within the Eclipse workspace. Watsonx Code Assistant is unlikely to successfully introspect your project if you import the top-level directory as a single project.
Language support
Test generation is available for the following languages:
- C
- C++
- Go
- Java
- JavaScript
- Python
- TypeScript
Dependencies
For Java, make sure that the pom.xml file includes the following dependencies:
- JUnit - A minimum of JUnit 5 is required
- Mockito - A minimum of Java 9 is required
The following sample pom.xml file shows the dependencies with example versions. In your file, include the dependencies and versions that you are using.
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.10.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>5.12.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-junit-jupiter</artifactId>
<version>5.12.0</version>
<scope>test</scope>
</dependency>
</dependencies>
Using a chat command to generate a unit test
You can use the /unit-test command in chat to generate a test for a referenced class, file, function, or method in the active workspace.
Use this syntax:
/unit-test <code reference> [additional instructions]
For <code reference>, type the @ symbol to see a list of classes, files, functions, and methods from your workspace. Use one class, file, function, or method reference at a time.
The [additional instructions] are optional. Add instructions if you want specific details.
Example prompts:
- File:
/unit-test @OrdersAlertFilter.java - Class:
/unit-test @OrdersAlertFilter - Method:
/unit-test @DoFilter()
Next, watsonx Code Assistant processes the request and in the chat displays the generated unit test for the code that you selected.
Using the CodeLens in the editor to generate unit tests
In the IDE editor, the CodeLens shows a line of generative AI options that precedes code blocks and snippets.
-
Click the Unit Test option that immediately precedes a code block to generate an explanation.
In the following code example, the
Explain | Document | Unit Testoptions immediately precede theprotected voidkeywords.CodeLens example
-
The watsonx Code Assistant chat window opens, displays the
/unit-test @<*item name*>command, runs the command, and displays the unit test.
Generating unit tests from the context menu
To generate a unit test from a context menu:
-
In the Explorer view, expand your application to the code that you want to generate a unit test for.
-
Right-click the code, click watsonx Code Assistant, then click Unit Test.
-
The watsonx Code Assistant chat displays the
/unit-testcommand for the code that you selected for unit testing. The following syntax is used in the command:/unit-test @<*item syntax*>The following syntax examples are for a file, a class, and a method.
- File:
/unit-test @OrdersAlertFilter.java - Class:
/unit-test @OrdersAlertFilter - Method:
/unit-test @DoFilter()
- File:
-
Watsonx Code Assistant processes the request and in the chat displays the generated unit test for the code that you selected.