Creating test configurations
Test configuration for a directory
Navigate to
.In the Run/Debug Configurations dialog, click Add New Configuration Alt+Insert and select Go Test.
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 followinggo test
arguments:Code
Description
go test -run
Run all tests.
go test -run Test
Run 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 theID=
pattern.go test -run /UID=1
Run 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: 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.
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 .
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.
Click Apply.
Test configuration for a package
If you want to create a run/debug configuration for a package, enusre 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.
Navigate to
.In the Run/Debug Configurations dialog, click Add New Configuration Alt+Insert and select Go Test.
From the Test kind list, select Package.
-
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 followinggo test
arguments:Code
Description
go test -run ''
Run all tests.
go test -run Test
Run 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 theID=
pattern.go test -run /UID=1
Run 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.
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 .
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.
Click Apply.
Test configuration for a file
Application code and test functions are in the same *_test.go file.
Navigate to
.In the Run/Debug Configurations dialog, click Add New Configuration Alt+Insert and select Go Test.
From the Test kind list, select File.
In the Files field, type a name of a testing file (for example, main_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.
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 .
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.
Click Apply.