GoLand 2020.1 Help

Create run/debug configurations for tests

Test configuration for a directory

An application file and a test file are in the same directory (for example, applicationFolder/main.go and applicationFolder/main_test.go). See main.go and main_test.go files in the GitHub repository.

  1. Navigate to Run | Edit Configurations.

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

  3. In the Directory field, specify a path to a directory that includes an application file and a test file (for example, applicationFolder/main.go and applicationFolder/main_test.go).

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

    • Pattern: arguments that you define for running subtests and sub-benchmarks. For more information about subtests and sub-benchmarks, see Subtests and Sub-benchmarks at golang.org. Consider the following test function:

      func MyTestFunc(t *testing.T) { ... t.Run("ID=1", func(t *testing.T) { ... }) t.Run("ID=2", func(t *testing.T) { ... }) t.Run("UID=1", func(t *testing.T) { ... }) ... }

      For the MyTestFunc function, you can use the following go test arguments:

      Code Description
      go test -runRun all tests.
      go test -run TestRun tests that match the Test pattern (for example, MyTestFunc).
      go test -run MyTestFunc/ID=Run subtests that belong to the top-level MyTestFunc function and match the ID= pattern.
      go test -run /UID=1Run subtests matching UID=1 from all top-level test functions.

      For the Pattern field, you list only arguments (for example, Test, MyTestFunc/ID=, /UID=1).

    • 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.

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

      To edit environment variables, click the Browse button (the Browse button). 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 Ctrl+Alt+S under Go | Build Tags & Vendoring.

    • Program arguments: arguments for the built application.

    • Run with sudo: grant sudo privileges for the application.

    • Before launch: Activate tool window: add tasks that you want to launch before the launch of your application. To add a task click the Add button Alt+Insert and select the tool that you want to add.

  4. Click Apply.

    Test configuration for a directory

Test configuration for a package

If you want to create a run/debug configuration for a package, ensure that an application file and a test file are in a package or in a collection of related Go packages (a module). Packages must be in the go/src directory (for example, go/src/myPackage). Modules are go.mod files that list all the related Go packages and requirements. See example files in the divisionMod directory in the GitHub repository.

To enable package tests, open setting by pressing Ctrl+Alt+S, navigate to Go | Go Modules (vgo), and select the Enable Go Modules integration checkbox.

  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 Package.

  4. In the Files field, type a name of a testing file (for example, main_test.go). You can use Smart Completion Ctrl+Shift+Space in this field.

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

    • Pattern: arguments that you define for running subtests and sub-benchmarks. For more information about subtests and sub-benchmarks, see Subtests and Sub-benchmarks at golang.org. Consider the following test function:

      func MyTestFunc(t *testing.T) { ... t.Run("ID=1", func(t *testing.T) { ... }) t.Run("ID=2", func(t *testing.T) { ... }) t.Run("UID=1", func(t *testing.T) { ... }) ... }

      For the MyTestFunc function, you can use the following go test arguments:

      Code Description
      go test -run ''Run all tests.
      go test -run TestRun tests that match the Test pattern (for example, MyTestFunc).
      go test -run MyTestFunc/ID=Run subtests that belong to the top-level MyTestFunc function and match the ID= pattern.
      go test -run /UID=1Run subtests matching UID=1 from all top-level test functions.

      For the Pattern field, you list only arguments (for example, Test, MyTestFunc/ID=, /UID=1).

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

    • 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 (the Browse button). 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 Ctrl+Alt+S under Go | Build Tags & Vendoring.

    • Program arguments: arguments for the built application.

    • Run with sudo: grant sudo privileges for the application.

    • Before launch: Activate tool window: add tasks that you want to launch before the launch of your application. 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 package

Test configuration for a file

Application code and test functions are in the same *_test.go file. See the main_file_test.go file in the the GitHub repository.

  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 File.

  4. In the Files field, type a name of a testing file (for example, /Users/jetbrains/runDebugConfigurationsForTests/main_file_test.go).

    Ensure that the Files field does not include other paths. Otherwise, you will get the Named files must all be in one directory error (for example, if the path looks like /Users/jetbrains/runDebugConfigurationsForTests/|/Users/jetbrains/runDebugConfigurationsForTests/main_file_test.go).

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

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

    • 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 (the Browse button). 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 Ctrl+Alt+S under Go | Build Tags & Vendoring.

    • Program arguments: arguments for the built application.

    • Run with sudo: grant sudo privileges for the application.

    • Before launch: Activate tool window: add tasks that you want to launch before the launch of your application. 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 file
Last modified: 17 July 2020