Testing Multiple Modules in a Parent Directory
When working with multiple Go modules in a parent directory, it can be challenging to run tests across all of them. Consider a directory structure like the one below:
/root /one go.mod go.sum main.go main_test.go /two go.mod go.sum main.go main_test.go
Issue:
Attempting to run tests for all modules under the parent directory using go test./... results in the error:
go: warning: "./..." matched no packages no packages to test
Solution:
The go test command is designed to work on a single module rooted in the current directory or its parent. It does not support testing nested modules.
To resolve this issue, you'll need to use a shell trick. One approach is to use a combination of find and xargs commands, as suggested by Cerise Limón's comment. Projects often create a Makefile or a test.sh script to automate this process.
For large projects with numerous modules, a more systematic approach is to maintain a list of all modules (as in Google's go-cloud project). The project then employs scripts that loop through the module list and perform tasks such as running tests for each directory.
Example:
The following test.sh script demonstrates this approach:
<code class="sh">#!/bin/bash for module in $(cat allmodules); do cd $module go test cd .. done</code>
Note:
You may not require a separate file to list the modules, but the go-cloud project's implementation provides an example of how larger projects manage multi-module testing.
The above is the detailed content of How to Test Multiple Go Modules in a Parent Directory?. For more information, please follow other related articles on the PHP Chinese website!