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
Release: 2024-11-30 19:44:14
Original
266 people have browsed it

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
Copy after login

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

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

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`
Copy after login

Skipping Subdirectories

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

go test `go list ./... | grep -v directoriesToSkip`
Copy after login

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 ./...
Copy after login

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!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template