Logo credit to golang.org

Automatic Testing in Go

Further adventures with metaprogramming in Go. In which, we build on our Golang metaprogramming tool.

Eric Lang
Published in
30 min readMar 4, 2020

--

This is a follow-on to the last article, Metaprogram in Go, probably start there if you have not read it yet. In this article, we will focus on automatically creating tests for our CRUD api. Recall that we are already automatically making the api themselves based on PostgreSQL table definitions. Along the way, we’ll also build a couple of utilities that will make our metaprogramming tool more useful.

Flushed with the excitement of automatically creating 100's of lines of compilable, runnable, bug-free* (* not really, keep reading) code from just a few lines of SQL table definition we are like hammers, looking for a nail. What more can we metaprogram? A lot, it turns out, but everything takes time. Where to start?

Let’s set four goals for improving on metaapi.

  1. Automatically create tests for our metaapi generated api
  2. Use internal templates for default code generation
  3. Support code generation outside of metaapi
  4. Automatically create an initial metaapi project

We’ll discuss all of these in more detail.

Automatic Tests

One thing missing from many projects is testing the code. Here is a question though — should you build tests for automatically generated code? You might assume that if the generator is working correctly, then it is generating bug-free code. Right?

Of course, you cannot assume that generated code is bug-free for the simple reason that generated code is a product of a generator, and the generator itself is still very subject to human error. Moreover, the generator writer is as obliged to provide automatic tests as if they were writing non-meta (normal?) programs. Consider these points:

  • The automatic tests become a contract and document of the extent to which the generated code has been tested.
  • The user may modify/extend the generated code over time and will need tests when the code changes.
  • The context (other code) that the generated code finds itself in may exercise it in…

--

--