Testing Nested Go Modules
Running go test can be challenging when working with multiple Go modules. The traditional approach of using go test./... will fail with an error indicating no matched packages or no packages to test.
This is because go test is designed to work on a single module, not multiple. To test nested modules, a different approach is required.
One solution involves using a shell trick to execute go test in each module individually. For example, you could use find to search for directories containing go.mod files and run go test within each of those directories:
find . -type d -name go.mod -exec go test {}
Alternatively, you can create a helper script or Makefile to iterate through the desired directories and run go test accordingly:
# test.sh
#!/bin/bash
for dir in */; do
if [ -f "$dir/go.mod" ]; then
go test "$dir"
fi
done
Some larger projects may maintain a list of all submodules and utilize scripts like the one in the example above to facilitate testing.
By employing these techniques, you can effectively run tests across multiple nested Go modules from a parent directory.
Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.
Copyright© 2022 湘ICP备2022001581号-3