IntelliJ IDEA 2023.1 Help

Testing

Testing ensures that behavior of your code is correct and expected. Good-written tests allow you to develop with confidence that your code will work and will not break your existing code.

IntelliJ IDEA includes several packages that you can use to run your tests. Also, you can apply a specific testing scope for the selected package.

Packages for testing

For testing purposes, IntelliJ IDEA includes the following packages:

gotest

Use for running standard unit tests. For more information about go test, see Package testing.

gocheck

Use to have extended functionality of go check and running more complex tests. For more information about go check, see go check.

gobench

Use for running performance tests. For more information about gobench, see Package testing: Benchmarks.

Packages for testing

Templates for Go tests work the same way as templates for Go applications (Go Build). But instead of selecting Go Build, you should select Go Test.

Run/debug configuration templates for tests

  1. Navigate to Run | Edit Configurations.

  2. In the Run/Debug Configurations dialog, click Add New Configuration Alt+Insert and select Go Test.

  3. From the Test kind list, select the scope from which you want to run tests:

    • Directory: to run all the tests in the specified directory. In the Directory field, specify a path to a directory that includes an application file and a test file (for example, applicationFolder/ with main.go and main_test.go).

    • Package: to run all the tests that belong to a package. In the Package path field, select a path to the package with tests that you want to run (for example, github.com/rcrowley/go-metrics).

      To enable package tests, open setting by pressing Control+Alt+S, navigate to Languages & Frameworks | Go | Go modules, and select the Enable Go modules integration checkbox.

    • File: to run all tests from a testing file. In the Files field, type a path to a testing file.

      Ensure that the Files field does not include other paths.

  4. (Optional) Also, you can specify the following settings:

    • Pattern: a regular expression that defines what tests to run. IntelliJ IDEA will run only those tests, examples, and fuzz tests that match the regular expression. For tests, the regular expression is split by unbracketed slash (/) characters into a sequence of regular expressions, and each part of a test's identifier must match the corresponding element in the sequence, if any.

    • Working directory: a directory that is used for the built application. If you have any code that creates relative files or directories, they will be relative to this directory.

    • Output directory: directory that stores your test results if any.

    • Run after build: execute a test after the build.

    • Redirect input from: a path to the file that will to take program input instead of the console.

    • Working directory: directory that is used for the built application. If you have any code that creates relative files or directories, they will be relative to this directory.

    • Environment: environment variables that you need to run the test.

      To edit environment variables, click the Browse button at the end of the field. In the Environment Variables dialog, click the Add button and add the environment variables that you need.

      Add an environment variable
    • Go tool arguments: arguments for the go tool (for example, -tags).

    • Use all custom build tags: all tags that are applied during the build. Tags are listed in settings Control+Alt+S under Languages & Frameworks | Go | Build Tags & Vendoring.

    • Program arguments: arguments for the test.

    • Run with sudo: grant sudo privileges to the test.

    • Before launch: Activate tool window: add tasks that you want to launch before the launch of the selected run/debug configuration. To add a task, click the Add button Alt+Insert and select the tool that you want to add.

  5. Click Apply.

    Test configuration for a directory
Last modified: 08 March 2021