Home > Net >  run go test command on multiple testfiles with go.work file
run go test command on multiple testfiles with go.work file

Time:10-25

As of go 1.18 it is possible to use "go.work" files as multimodule workspace save file, equivalent to a "go.mod" file.

I want to know how to use the defined "go.work" file in the command go test.

I tried multiple approaches. When I print the go.work env variables everything seems to work.

How do I use the "go test" command on a "go.work" file?

lets assume i have a directory with two subdirectories with each a go.mod file and two .go source files (file.go and file_test.go) and the main directory with just the go.work file.

go test -v .\go.work --> not possible

go test ".\ ..." --> doesn't work as specified, it can't resolve it correctly.

The flag build.experimentalWorkspaceModule is set to false because it isn't needed anymore. Just go test all seems to work, but I just want to test my packages, not the whole packages installed.

I dug into the whole documentation, I cant find an example because it seems to be new and not used frequently, the pkg.go.dev doesn't supply info.

Also not here https://go.googlesource.com/tools/ /refs/heads/master/gopls/doc/workspace.md

CodePudding user response:

I don't think go.work can be used to this end. Workspaces is a part of the modules system, and are basically used to make it easier (for you) to set up a local multi-module repository. The following quote is from the proposal:

The go.work file specifies a set of local modules that comprise a workspace. When invoked in workspace mode, the go command will always select these modules and a consistent set of dependencies.

And this one from the workspace tutorial:

go.work can be used instead of adding replace directives to work across multiple modules.

And finally this one from the official docs:

A workspace is a collection of modules on disk that are used as the main modules when running minimal version selection (MVS).

Running the test suite of several modules at once is orthogonal to go.work's use case. For that, you still have to use a shell script (see Running `go test` in parent directory of multiple go modules) or similar.

  • Related