PyCharm 2024.1 Help

Generate tests with AI

With AI Assistant, you can generate unit tests for code segments. AI Assistant analyzes both your code and its documentation to recommend tests that will provide insights into your code's behavior.

  1. Place the caret somewhere within a method and right-click to open the context menu.

    Alternatively, press Alt+Enter.

  2. Select AI Actions and then Generate Unit Tests.

    Generate Unit Tests action in context menu
  3. AI Assistant generates tests for the default test runner of your project. The generated tests open in a separate AI Diff tab.

    Generated unit tests in a separate AI Diff tab

    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.

  4. Click Accept all to save the generated test.

    It will be shown as a new file in a separate tab.

    Generated test saved as a new file

    If you already have a tests directory in your project, the new test will be stored there. Otherwise, the directory will be created.

Last modified: 05 April 2024