unit-testinggotesting

Running `go test` in parent directory of multiple go modules


Take a look at this directory structure:

/root
    /one
        go.mod
        go.sum
        main.go
        main_test.go
    /two
        go.mod
        go.sum
        main.go
        main_test.go

Let's say I'm in the parent directory root/ and I want to run all the tests in all subdirectories. What do I do? I've tried go test./... and got the following error:

go: warning: "./..." matched no packages
no packages to test

Solution

  • Yes, this will not work with go test, which expects to work on a single module rooted in the current directory or its parent. It won't work with nested modules either.

    You'll need a shell trick like using find or something equivalent to execute in each module (see Cerise Limón's comment for example). Projects will typically use a Makefile or a test.sh script to run that.


    One larger project I was working on has a list of all its modules (https://github.com/google/go-cloud/blob/master/allmodules) and then several scripts that operate on this list. For example the test script just loops through this file and runs go test for each directory, along other things.

    You don't necessarily need a separate file listing the modules (the go-cloud project uses that for other management tasks) but it just demonstrates one way large projects with multiple modules handle things.