ReSharper 2023.3 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.

Create unit test for a class

  1. In the editor, place the caret at a class that you want to create a unit test for.

  2. Press Control+T, C, or alternatively open the Generate Code Alt+Insert menu and choose Unit Test.

  3. If your solution contains a test project with a test class that matches the name of your class, ReSharper will add a new test method in that class using the corresponding live template. Note that in version 2023.3, generation of testing logic with AI is not supported in this case. This will be fixed in the next version. As a workaround, rename the existing test class.

    Otherwise, the Create Unit Test dialog will open:

    ReSharper: Create Unit Test dialog
  4. In the dialog, specify where and how the test should be created:

    • Test project: Type in a name for a new project or choose an existing test project from the drop-down selector.

    • Test framework: Choose one of the supported unit test frameworks (NUnit, xUnit, or MSTest). In the selector, you will see, which framework versions are already used in the target project or in your solution — it is recommended to select these versions for consistency. However, you can also select the latest version of the desired framework, and it will be installed from NuGet. The framework will be referenced in the target project, and the test class will be created from a file template corresponding to that framework.

    • Test class name: The name for the test class will be automatically suggested according to the pattern that you can configure under Test class naming convention on the Tools | Unit Testing | Test Linker page of ReSharper options . It is recommended to use the suggested name because ReSharper will use it to link the class and its tests when you navigate to and from the test.

    • Generate test content with AI: Select this checkbox to generate actual testing logic for the created test.

    • Destination (Advanced): Use this field to specify a custom relative path to the file where the test class will be created.

    • Namespace (Advanced): Use this field to specify a custom namespace for the created test class.

    • Template (Advanced): Use this field to choose a file template that will be used for the created test class.

  5. Click OK to create the test. ReSharper will populate the test class with one or more test methods that will check the logic of the originally selected class.

    Note that there may be some unresolved symbol references in the generated code because of missing project references or using directives. In most cases, these are easy to fix: press Alt+Enter on a highlighted reference and choose Import missing references in file.

Last modified: 24 January 2024