---
title: How to Unit Test a GO Service on Google App Engine - Part 3
description: Part three of a series on Google App Engine that shows how to perform a unit test on a GO service.
image: https://blog.avenuecode.com/hubfs/pcosta/teste_gae+go%20(3).png
---

[AvenueCode.com](https://avenuecode.com) [News](https://avenuecode.com/news) [Contact](https://avenuecode.com/contact)

[![Avenue Code Snippets Logo](https://blog.avenuecode.com/hubfs/Avenue%20Code%20New%20Logos%20-%202023/AC-Snippets---Black.png)](https://blog.avenuecode.com/?hsLang=en-us) *menu*

- Technology
  
  [Cloud](https://blog.avenuecode.com/blog/topic/cloud?hsLang=en-us) [Delivery Infrastructure](https://blog.avenuecode.com/blog/topic/delivery-infrastructure?hsLang=en-us) [Web Experience](https://blog.avenuecode.com/blog/topic/web-experience?hsLang=en-us) [Agile Mindset](https://blog.avenuecode.com/blog/topic/agile-mindset?hsLang=en-us) [Quality First](https://blog.avenuecode.com/blog/topic/quality-first?hsLang=en-us)
  
  [Design](https://blog.avenuecode.com/blog/topic/design?hsLang=en-us) [Solution Architecture](https://blog.avenuecode.com/blog/topic/solution-architecture?hsLang=en-us) [Data & ML](https://blog.avenuecode.com/blog/topic/data-and-machine-learning?hsLang=en-us) [Mobile Experience](https://blog.avenuecode.com/blog/topic/mobile-experience?hsLang=en-us)
- [Whitepapers](https://blog.avenuecode.com/blog/topic/whitepapers?hsLang=en-us)
- [Spotlight](https://blog.avenuecode.com/blog/topic/spotlight?hsLang=en-us)
- [Extraordinary Women in Tech](https://blog.avenuecode.com/blog/topic/extraordinary-women-in-tech?hsLang=en-us)
- [Avenue Code Culture](https://blog.avenuecode.com/blog/topic/avenue-code-culture?hsLang=en-us)

- [Cloud](https://blog.avenuecode.com/blog/topic/cloud?hsLang=en-us)
- [Design](https://blog.avenuecode.com/blog/topic/design?hsLang=en-us)
- [Delivery Infrastructure](https://blog.avenuecode.com/blog/topic/delivery-infrastructure?hsLang=en-us)
- [Solution Architecture](https://blog.avenuecode.com/blog/topic/solution-architecture?hsLang=en-us)
- [Web Experience](https://blog.avenuecode.com/blog/topic/web-experience?hsLang=en-us)
- [Data & ML](https://blog.avenuecode.com/blog/topic/data-and-machine-learning?hsLang=en-us)
- [Agile Mindset](https://blog.avenuecode.com/blog/topic/agile-mindset?hsLang=en-us)
- [Mobile Experience](https://blog.avenuecode.com/blog/topic/mobile-experience?hsLang=en-us)
- [Quality First](https://blog.avenuecode.com/blog/topic/quality-first?hsLang=en-us)
- [Whitepapers](https://blog.avenuecode.com/blog/topic/whitepapers?hsLang=en-us)
- [Spotlight](https://blog.avenuecode.com/blog/topic/spotlight?hsLang=en-us)
- [Extraordinary Women in Tech](https://blog.avenuecode.com/blog/topic/extraordinary-women-in-tech?hsLang=en-us)
- [Avenue Code Culture](https://blog.avenuecode.com/blog/topic/avenue-code-culture?hsLang=en-us)

# [How to Unit Test a GO Service on Google App Engine - Part 3](https://blog.avenuecode.com/how-to-unit-test-a-go-service-on-google-app-engine-part-3)

- [Tweet](https://twitter.com/share)

*perm\_identity* [Pedro Costa](https://blog.avenuecode.com/how-to-unit-test-a-go-service-on-google-app-engine-part-3/author/pedro-costa?hsLang=en-us)

*schedule* 12/13/17 6:00 PM

*This article is part of a series of articles on GO development on Google Cloud. Read the [first part](https://blog.avenuecode.com/go-appengine-hello-world?hsLang=en-us) to learn about project creation on Google App Engine, and the [second part](https://blog.avenuecode.com/go-rest-service-on-google-appengine?hsLang=en-us) 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](https://golang.org/pkg/testing/#hdr-Main):

*"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
```

 

### Conclusion

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.

---

[![LinkedIn Icon](https://blog.avenuecode.com/hubfs/Images/Blog/linkedin.png)](https://www.linkedin.com/in/pedrocelso)

### Author

# 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.

---

### Related Posts

### Building Accessible Web Applications

[READ MORE](https://blog.avenuecode.com/building-accessible-web-applications?hsLang=en-us)

### Create a Tunnel from Anypoint Platform to GCP Using an HA VPN

[READ MORE](https://blog.avenuecode.com/create-a-tunnel-from-anypoint-platform-to-gcp-using-ha-vpn?hsLang=en-us)

### Create a Classic Tunnel from GCP to Anypoint Platform

[READ MORE](https://blog.avenuecode.com/create-a-classic-tunnel-from-gcp-to-anypoint-platform?hsLang=en-us)

### How to Make an Android Splash Screen

[READ MORE](https://blog.avenuecode.com/how-to-make-an-android-splash-screen?hsLang=en-us)

### Leave a Comment!

### Avenue Code Social

[![Facebook Icon](https://blog.avenuecode.com/hubfs/Images/Blog/facebook.png?t=1486470796564)](https://www.facebook.com/avenuecode)

[![Twitter Icon](https://blog.avenuecode.com/hubfs/Images/Blog/twitter.png?t=1486470796842)](https://twitter.com/AvenueCode)

[![LinkedIn Icon](https://blog.avenuecode.com/hubfs/Images/Blog/linkedin.png?t=1486470796556)](https://www.linkedin.com/company/avenue-code)

### Newsletter

Want to stay on top of all tips and news from Avenue Code?

### Popular Snippets

![Primary Logo AvenueCode Inverted.png](https://blog.avenuecode.com/hs-fs/hubfs/Images/Logos/Primary%20Logo%20AvenueCode%20Inverted.png?width=1104&name=Primary%20Logo%20AvenueCode%20Inverted.png "Primary Logo AvenueCode Inverted.png")

### About Us

- [Who We Are](https://www.avenuecode.com/who-we-are)
- [What We Do](https://www.avenuecode.com/what-we-do)
- [Portfolio](https://www.avenuecode.com/portfolio)
- [Partners](https://www.avenuecode.com/partners)
- [News](https://www.avenuecode.com/news)
- [Events](https://www.avenuecode.com/events)
- [Blog](https://blog.avenuecode.com/)
- [Contact](https://www.avenuecode.com/contact)

### Our Offices

San Francisco

[+1 415 766 4178](tel:+553125161448) [ac.inquiries@avenuecode.com](mailto:brazil.info@avenuecode.com)

Belo Horizonte

[+55 31 2516 1448](tel:+553125161448) [brazil.info@avenuecode.com](mailto:brazil.info@avenuecode.com)

São Paulo

[+55 11 3205 3232](tel:+553125161448) [brazil.info@avenuecode.com](mailto:brazil.info@avenuecode.com)

### We're Hiring!

- [Belo Horizonte](https://www.avenuecode.com/who-we-are)
- [New York](https://www.avenuecode.com/what-we-do)
- [San Francisco](https://www.avenuecode.com/portfolio)
- [São Paulo](https://www.avenuecode.com/partners)

---

©2015 - 2017 Avenue Code

[![Facebook Icon](https://blog.avenuecode.com/hubfs/Images/Icons/facebook-2.png)](https://www.facebook.com/avenuecode) [![Twitter Icon](https://blog.avenuecode.com/hubfs/Images/Icons/twitter-2.png)](https://twitter.com/AvenueCode) [![LinkedIn Icon](https://blog.avenuecode.com/hubfs/Images/Icons/linkedin-2.png)](https://www.linkedin.com/company/avenue-code) [![Glassdoor Icon](https://blog.avenuecode.com/hubfs/Images/Icons/glassdoor-icon-1.png)](https://www.glassdoor.com/Overview/Working-at-Avenue-Code-EI_IE456173.11,22.htm) [![YouTube Icon](https://blog.avenuecode.com/hubfs/Images/Icons/youtube-2.png)](https://www.youtube.com/user/AvenueCodePlay)

Please enable JavaScript to view the [comments powered by Disqus.](http://disqus.com/?ref_noscript)

© 2026 Avenue Code

```json
{
  "@context" : "https://schema.org",
  "@type" : "BlogPosting",
  "author" : {
    "@type" : "Person",
    "name" : "Pedro Costa",
    "url" : "https://blog.avenuecode.com/author/pedro-costa"
  },
  "datePublished" : "2017-12-13T20:00:00.000Z",
  "headline" : "How to Unit Test a GO Service on Google App Engine - Part 3",
  "image" : [ "https://blog.avenuecode.com/hubfs/pcosta/teste_gae+go%20(3).png" ],
  "mainEntityOfPage" : {
    "@id" : "https://blog.avenuecode.com/how-to-unit-test-a-go-service-on-google-app-engine-part-3",
    "@type" : "WebPage"
  },
  "publisher" : {
    "@type" : "Organization",
    "logo" : {
      "@type" : "ImageObject",
      "url" : "https://blog.avenuecode.com/hubfs/Avenue%20Code%20New%20Logos%20-%202023/Avenue%20Code-primary%20versions_LOGO%20HORIZONTAL%20group%201-7.png"
    },
    "name" : "Avenue Code"
  }
}
```