Home > Backend Development > Golang > How to Run `go test` in the Parent Directory of Multiple Go Modules?

How to Run `go test` in the Parent Directory of Multiple Go Modules?

Barbara Streisand
Release: 2024-10-29 03:09:29
Original
359 people have browsed it

How to Run `go test` in the Parent Directory of Multiple Go Modules?

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

Running go test./... from the root directory will result in the error:

go: warning: "./..." matched no packages
no packages to test
Copy after login

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

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

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!

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