Good way to use and test local packages in Google App Engine apps written in golang?
I have a problem testing my GAE golang application using local packages. The task looks something like this.
.
βββ app.yaml
βββ main.go
βββ handler
βββ handler.go
handler
the package is imported into main.go
.
package main
import "handler"
Everything (for example goapp serve
) works fine until I start writing tests.
goapp test
complains that the package was handler
not found. It seems that GOPATH
from goapp serve
and are goapp test
different. One solution I found was to put the
handler
package outside of the project path and import it completely (for example github.com/.../handler
), but it doesn't make sense to me to split the project into separate places where they are closely related. Is there any good way to use and test local packages?
The following resources are found in this thread.
source to share
It happens late, but if someone is facing the same problem, here's how to deal with it:
-
Organize your code this way
/whereever βββ /myproject βββ/src βββ app.yaml βββ main.go βββ /handler βββ handler.go
-
Use unqualified imports for project code eg.
import handler
-
Run
goapp serve
andgoapp deploy
from the src folder. serve / deploy work relative to app.yamlcd /whereever/myproject/src goapp serve
-
Install GOPATH for
goapp test
, it works based on GOPATHcd /whereever/myproject #GOPATH expects a subfolder called src GOPATH=$GOPATH:`pwd` #add the current folder to the GOPATH goapp test ./... #run all tests in any subfolders
-
Reset your GOPATH if needed
source ~/.profile #this assumes ~/.profile is where you have permanently set your GOPATH
Overall, "work as intended," but still a pain.
source to share
You need to import the fully qualified handler, but you don't need to move it out of the project to do this. If the project folder looks like this:
/go
βββ /myproject
βββ app.yaml
βββ main.go
βββ /handler
βββ handler.go
Then the import of your handler should look like this:
import "myproject/handler"
source to share