Go Test

Golang Database Integration Test with Github Action

MySQL Integration Test Made Easy with Github Action

David Yappeter

--

source: https://github.com/marcusolsson/gophers

In the previous article (Golang MySQL Integration Test), we have created a go application with Integration Test to MySQL. If you haven’t read it, I suggest you read that article first, then continue to this article.

So, let’s start with a little introduction about Github Action.

What is Github Action?

Github Action is workflow automation that helps you automate software test, build, and even deployment and comes with a great list of community workflows and services. You can set when to trigger the Action for example by a push, pull-request, etc., and even set the branch name. Other similar CI/CD tools for example CircleCI, TravisCI, GitlabCI, Jenkins, etc.

This time we will combine the usage of Github Action with our Go code.
If you are wondering what is Integration Test, it is a test that combining some section of our application that is part of our program flow and making sure the interaction between sections is correct. For example, user register flow. The users cannot register 2 duplicate emails, etc.

Requirement

We will use the latest repository from the previous article, which already contains the code and test that we need. Of course, you can create your own project.

Repository: https://github.com/david-yappeter/go-mysql-suite

Github Action Config

The path of the configuration file must be in .github/workflow/ folder, we will name it action.yml

.github/workflow/action.yml

.github/workflows/action.yml

Let’s break it into some parts:

name: action , this is the name of your workflow, later on, then we will set it triggered on push and on pullrequest on master branches .

jobs will contain the main flow of our workflows. We give the job name build and define it runs-on: ubuntu-latest which is ubuntu-20.04 which has a build-in Mysql service that we can use.

Our job environment contains database default database configuration settings in the runner, the user and the password must be root and change the database according to your Integration Test code environment.

Next, every job will have multiple steps . First, we start our Mysql service which located in /etc/init.d/mysql then login into the MySQL by using Mysql Client and Create DATABASE according to the environment.

After that, we will checkout our code to the runner by using actions/checkout@v2 and add a Go executable with actions/setup-go@v2, select the right version for your code. The last step is to test and build our code by using normal go syntax: go test and go build .

Artifact is an optional step, it will take our executable file later and display it in the GitHub action, so we can download it later on.

Let’s try to push this code up to the master branch and see the Action tab of your repository.

GitHub Action

As we can see, the workflow works perfectly fine. If there is an error, GitHub will notify your fail workflow via email. Go back to the previous page, and we can see the build artifact on the bottom section of the page that can be download.

Artifact

Lastly, we can add the workflow status to our readme as a badge.

[![build](https://github.com/david-yappeter/go-mysql-suite/actions/workflows/action.yml/badge.svg)](https://github.com/david-yappeter/go-mysql-suite/actions/workflows/action.yml)

This is the sample of my Github action badge for this repository, change the link according to your repository name.

[![build]($repository/actions/workflows/$workflow_file/badge.svg)]

That’s all for this article, hope you have a nice day :).

--

--