With AI Assistant, you can generate unit tests for code segments (including public methods in Ruby files, PHP, or C# methods). AI Assistant analyzes both your code and the code segment context to recommend tests that will provide insights into your code's behavior.
Generate unit tests
Place the caret somewhere within a class or a specific method and right-click to open the context menu.
Alternatively, press AltEnter.
Select AI Actions and then Generate Unit Tests.
The generated test opens in a separate AI Diff tab.
note
In PyCharm, AI Assistant generates tests for the default test runner of your project (Settings | Tools | Python Integrated Tools | Testing | Default test runner).
If you want to improve the generated code, click Specify, add new requirements, and press Enter.
If you want to regenerate the answer, click Regenerate.
If you want to modify the prompt, click Customize Prompt and provide new instructions. The changes will take effect from the next test generation attempt.
Click Accept all to save the generated test.
It will be shown as a new file in a separate tab.
If you already have a test module in your project, the new test will be stored there. If not, AI Assistant will automatically create a test module and place the generated test inside it.
If a test file already exists, AI Assistant will locate it and add the generated tests to that file.
If you invoke the Generate Unit Tests action directly in a test file, AI Assistant will prompt you to provide additional details about the scenario you want to test. The generated code will then be added to the existing tests within the file.
tip
For IntelliJ IDEA: If you are developing a custom plugin, and you want to enhance or fine-tune the test generation functionality provided by AI Assistant, you can do so by using the com.intellij.ml.llm.generateTestsSupportextension point.