This article is part of a series of articles on GO development on Google Cloud. Read the first part to learn about project creation on Google App Engine, and the second part to learn about how to implement a basic REST service to run on App Engine environment.

In the previous article, we went over how to persist data on Google DataStore using GO. The final step we need to complete in order to finish the service is write the unit tests that check the stored/retrieved data from datastore. It's actually best to write the unit tests before the service (TDD), but for the sake of readability, I wrote the service first.

In order to test datastore functions locally, we need to have GAE instances/contexts mocked. To help developers, Google released a special package that leverages local unit testing called aetest. With that package we can mock new contexts and app engine instances at will, but keep in mind that this has a cost because it will really start a GAE service. To make test execution faster, we’ll create one shared context that will be used across different test functions:

 According to the testing package documentation:

"It is sometimes necessary for a test program to do extra setup or teardown before or after testing. It's also sometimes necessary for a test to control which code runs on the main thread. If a test file contains a TestMain function, then the generated test will call TestMain(m) instead of running the tests directly. TestMain runs in the main goroutine and can do whichever setup or teardown is necessary around a call to m.Run. It should then call os.Exit with the result of m.Run. When TestMain is called, flag.Parse has not been run. If TestMain depends on command-line flags, including those of the testing package, it should call flag.Parse explicitly."

We’ve created a shared context and executed a func that seeds datastore with 5 dummy Users. Now we can use this shared context (mainCtx) on the Users tests:

As you can see, I'm basically testing the "happy-path" and some edge cases for every exported method on User service. There's one more test to be added which is the test that will check the user.GetUsers method. This is a special test because it requires a new context in order to ensure that other tests won't affect this one:


Finally, to run your test:

go test lib/services/user/users_test.go



And that's how you unit test GO services that run on Google App Engine! I hope I've helped other developers see the simplicity that Google offers on its PaaS. As I mentioned in the first article, my main goal was to create the project and start writing  good code. From this point on, you can add your test execution to a CI server, or run it manually, and if the test passes, you can use the gcloud tool to deploy your application.


Pedro Costa

Programming isn’t about the language, it’s about the solution. In my 10+ years working in the software industry, I've had great opportunities to work on all types of applications. I've developed a couple of mobile apps and a few Java systems. I've also had the opportunity to write automated tests for a couple of applications. I’m always eager to learn new technologies/methodologies. My motivation is to wake up every day and be better than I was the day before.

How to Make an Android Splash Screen


AC Spotlight - Angela Hsu


AC Spotlight - Justin Sellman