Home >Backend Development >Golang >How Can I Run `go test` Across Multiple Directories?

How Can I Run `go test` Across Multiple Directories?

Mary-Kate Olsen
Mary-Kate OlsenOriginal
2025-01-04 08:21:35613browse

How Can I Run `go test` Across Multiple Directories?

Testing Multiple Directories with go test

In the Go testing framework, go test typically executes tests in a single directory containing *_test.go files. However, there may be instances where you need to test across multiple directories for a comprehensive project coverage.

To achieve this, you can use the following commands:

  • Testing Current Directory and Subdirectories:
go test ./...

This command instructs go test to run tests in the current directory and all of its children directories, recursively.

  • Testing Specific Directories:
go test ./tests/... ./unit-tests/... ./my-packages/...

This command allows you to specify multiple directories where you want the tests to run.

  • Testing Import Path Prefixed with a Namespace:
go test foo/...

If your project is organized in a hierarchy, you can use this command to run tests for all directories prefixed with the foo/ import path.

  • Testing Import Path Prefixed with a Namespace (Shortened):
go test foo...

This shortened version achieves the same result as the previous example, by matching all import paths that start with foo.

  • Testing All Tests in the GOPATH:
go test ...

Finally, to test all the tests in your $GOPATH, you can use this command, which includes all the Go modules you have installed in your system.

The above is the detailed content of How Can I Run `go test` Across Multiple Directories?. 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