Running go test in Parent Directory of Multiple Go Modules
When encountering a directory structure with multiple Go modules located in subdirectories, running go test from the parent directory can pose a challenge. The following code snippet demonstrates this issue:
/root /one go.mod go.sum main.go main_test.go /two go.mod go.sum main.go main_test.go
Running go test./... from the root directory will result in the error:
go: warning: "./..." matched no packages no packages to test
This occurs because go test is specifically designed to operate on a single module located either in the current directory or its parent. It does not support nested modules or executing tests from a parent directory of multiple modules.
To resolve this, a solution is to create a shell script or use a utility like find to navigate to each individual module and execute go test within those directories. For example:
cd /root/one go test . cd /root/two go test .
Alternatively, some projects may utilize a Makefile or test.sh script to automate this process. For instance, the test.sh script in the following hypothetical project loops through a list of modules and runs go test for each directory:
#!/bin/bash modules="one two three" for module in $modules; do cd $module go test . cd .. done
The above is the detailed content of How to Run `go test` in the Parent Directory of Multiple Go Modules?. For more information, please follow other related articles on the PHP Chinese website!