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



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

), 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

2 answers

It happens late, but if someone is facing the same problem, here's how to deal with it:

  • Organize your code this way

    └── /myproject
           β”œβ”€β”€ app.yaml
           β”œβ”€β”€ main.go
           └── /handler
               └── handler.go

  • Use unqualified imports for project code eg.

    import handler

  • Run goapp serve

    and goapp deploy

    from the src folder. serve / deploy work relative to app.yaml

    cd /whereever/myproject/src
    goapp serve 

  • Install GOPATH for goapp test

    , it works based on GOPATH

    cd /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.



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:

└── /myproject
    β”œβ”€β”€ app.yaml
    β”œβ”€β”€ main.go
    └── /handler
        └── handler.go


Then the import of your handler should look like this:

import "myproject/handler"




All Articles