How to Test Multiple Go Modules in a Parent Directory?

Barbara Streisand
Release: 2024-10-30 03:19:03
Original
955 people have browsed it

How to Test Multiple Go Modules in a Parent Directory?

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

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

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

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!

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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!