当测试文件在模块中时,如何运行“去测试”?

When you have pulled in a module, into your project, how do you run test cases within that module?

i.e. I have done:

go get my.repo.com/repo/mymodule

And then try to test something in it:

server> go test src/my.repo.com/repo/mymodule/my_test.go 
# command-line-arguments
src/my.repo.com/repo/mymodule/article_test.go:4:2: cannot find package "mymodule" in any of:

Are we supposed to check out our modules separately and test that way? I can't quite work out what to do. It seems that when I go run it knows how to find the module I have fetched, but when I go test, it "can't find it" in the path.

go test expects packages, not folders relative to $GOPATH.

So leave out the leading src/ and the trailing file name, and it'll work:

go test my.repo.com/repo/mymodule

If the current directory is the package folder you want to test, you can even omit it, e.g.

cd $GOPATH/src/my.repo.com/repo/mymodule
go test

For reference see Command go / Test packages, also run

go help test