Home >Backend Development >Golang >How Can I Customize Go Test Execution to Include or Exclude Specific Packages and Subdirectories?

How Can I Customize Go Test Execution to Include or Exclude Specific Packages and Subdirectories?

Patricia Arquette
Patricia ArquetteOriginal
2024-11-30 19:44:14267browse

How Can I Customize Go Test Execution to Include or Exclude Specific Packages and Subdirectories?

Customizing Test Execution: Skipping Specific Packages

Go test offers flexibility in selecting packages for testing. While running go test for each package individually is an option, customizing the execution to include only desired packages is a more efficient approach.

Testing Specific Subdirectories

In the provided directory structure, you can test specific subdirectories by explicitly listing their import paths on the command line:

go test import/path/to/mypackage import/path/to/mypackage/other import/path/to/mypackage/net

Alternatively, for package paths starting with a common prefix, brace expansion can be used:

go test import/path/to/mypackage{,/other,/net}

Using go list as Argument

You can employ go list to generate a list of packages as arguments for go test:

go test `go list`

Skipping Subdirectories

To exclude a subdirectory like mypackage/scripts, utilize grep to filter out undesired paths:

go test `go list ./... | grep -v directoriesToSkip`

Conditional Test Skipping

Long or expensive tests can be skipped conditionally within the tests themselves using testing.Short() and t.Skip(). This allows specific tests to be skipped via commands like:

go test -short import/path/to/mypackage/...

(within mypackage directory)
go test -short ./...

Custom conditions besides testing.Short() can also trigger test skipping, providing flexibility in controlling which tests to execute.

The above is the detailed content of How Can I Customize Go Test Execution to Include or Exclude Specific Packages and Subdirectories?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn