7

Below is my project structure:

/user/home/go/src/github.com/my_go_proj
                                       /main.go
                                       /mypackage
                                                 /service.go
                                                 /service_test.go

GOPATH points to /user/home/go
cd /user/home/go/src/github.com/my_go_proj
go build ---> This works and creates the `my_go_proj` executable.
go test
?       github.com/my_go_proj   [no test files]
go test github.com/my_go_proj/mypackage
go build gopkg.in/tomb.v2: no buildable Go source files in 
FAIL    github.com/my_go_proj/mypackage [build failed]

go test ./...
?       github.com/my_go_proj   [no test files]
go build gopkg.in/tomb.v2: no buildable Go source files in 
FAIL    github.com/my_go_proj/mypackage [build failed]

How do I run go test to run the service_test.go test inside mypackage?

Update: updated the behaviour for go test ./...

suman j
  • 6,710
  • 11
  • 58
  • 109
  • does `go test ./...` work? (and what does `go build gopkg.in/tomb.v2` have to do with this?) – JimB Dec 04 '15 at 16:57
  • Potential duplicate of [How to `go test` all testings in my project?](http://stackoverflow.com/questions/16353016/) and [Golang tests in sub-directory](http://stackoverflow.com/questions/19200235/). – Matthew Rankin Dec 04 '15 at 17:06
  • I have no idea where gopkg.in/tomb.v2 is coming from. `go test ./...` also did not work. – suman j Dec 04 '15 at 17:49
  • 1
    Something is trying to build "gopkg.in/tomb.v2", have you tried getting that dependency? – JimB Dec 04 '15 at 17:58
  • @MatthewRankin It is not a duplicate :) – suman j Dec 07 '15 at 19:55

2 Answers2

1

To test all subdirectories use:

$ go test ./...

From the Relative import paths section of the Command go documentation:

Relative patterns are also allowed, like go test ./... to test all subdirectories. See 'go help packages' for details on the pattern syntax.

Matthew Rankin
  • 457,139
  • 39
  • 126
  • 163
0

The problem was with the directory structure. My project source should be in $GOPATH. But it was inside $GOPATH/src.

Incorrect /user/home/go/src/github.com/my_go_proj
Correct /user/home/go/github.com/my_go_proj

suman j
  • 6,710
  • 11
  • 58
  • 109