


Seamless Integration Testing for Your Go Application on GitHub Actions with PostgreSQL
Introduction
Integration testing is crucial for ensuring your Go application works flawlessly with external dependencies like databases. In this blog, we’ll explore how to set up and run integration tests for a Go application using GitHub Actions. We’ll configure a PostgreSQL database within the CI pipeline, streamline the test process, and ensure your codebase is reliable and production-ready with every push. Let’s dive in!.
We created unit test and integrations in a previous article here!. In this article we want to run these tests on all commits to our github repository.
Github Actions
They are a continuous integration and continuous delivery (CI/CD) platform that allows you to automate your build, test and deployment pipeline.
Github Actions lets you run workflows when other events happen in your repository
Github Workflows
A workflow is a configurable automated process that will run one or more jobs. Workflows are defined by a YAML file checked in to your repository and will run when triggered by an event in your repository. Workflows are defined in the .github/workfows.
- Event is a specific activity in a repository that triggers a workflow run. In our case this will be a push to our branch.
- Jobs is a set of steps in a workflow that is executed on the same runner.
- Runners is a server that runs your workflows when they are triggered. Each runner can runner a single job at a time.
Workflow Yaml
- The first step would be to create .github/workflows folder where our yaml file will be located.
- Next is to create the yaml file in this case we will name it ci-test.yml.
name: ci-test on: push: branches: [main] pull_request: branches: [main] env: POSTGRES_USER: postgres POSTGRES_PASSWORD: Password123 POSTGRES_DB: crud_db jobs: build: name: tests runs-on: ubuntu-latest services: postgres: image: postgres env: POSTGRES_USER: ${{ env.POSTGRES_USER }} POSTGRES_PASSWORD: ${{ env.POSTGRES_PASSWORD }} POSTGRES_DB: ${{ env.POSTGRES_DB }} ports: - 5432:5432 options: >- --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 5 steps: - uses: actions/checkout@v4 - name: Set up Go uses: actions/setup-go@v4 with: go-version: "1.22" - name: Install dbmate for golang migrations run: | sudo curl -fsSL -o /usr/local/bin/dbmate https://github.com/amacneil/dbmate/releases/latest/download/dbmate-linux-amd64 sudo chmod +x /usr/local/bin/dbmate which dbmate - name: Construct DB URL id: construct_url run: echo "DB_URL=postgres://${{ env.POSTGRES_USER }}:${{ env.POSTGRES_PASSWORD }}@localhost:5432/${{ env.POSTGRES_DB }}?sslmode=disable" >> $GITHUB_ENV - run: env - name: Make Migrations run: make migrations URL=${{ env.DB_URL }} - name: Seed test DB run: go run db/seed.go - name: Test run: make test
Yaml Description
- The first part is to name the action in this case it is ci-test.
Workflow Triggers
- The second section describes triggers. Events that trigger the action. In this file we have two events that will trigger the running of this jobs, pushes and pull requests targeting the main branches. This ensures that every code change intended for production is tested before merged, maintaining the integrity of the project.
Environment Variables
Github workflows support global and job-specific environment variables. This variables describe postgres credentials that we will user later in our yaml file.
Job
name: ci-test on: push: branches: [main] pull_request: branches: [main] env: POSTGRES_USER: postgres POSTGRES_PASSWORD: Password123 POSTGRES_DB: crud_db jobs: build: name: tests runs-on: ubuntu-latest services: postgres: image: postgres env: POSTGRES_USER: ${{ env.POSTGRES_USER }} POSTGRES_PASSWORD: ${{ env.POSTGRES_PASSWORD }} POSTGRES_DB: ${{ env.POSTGRES_DB }} ports: - 5432:5432 options: >- --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 5 steps: - uses: actions/checkout@v4 - name: Set up Go uses: actions/setup-go@v4 with: go-version: "1.22" - name: Install dbmate for golang migrations run: | sudo curl -fsSL -o /usr/local/bin/dbmate https://github.com/amacneil/dbmate/releases/latest/download/dbmate-linux-amd64 sudo chmod +x /usr/local/bin/dbmate which dbmate - name: Construct DB URL id: construct_url run: echo "DB_URL=postgres://${{ env.POSTGRES_USER }}:${{ env.POSTGRES_PASSWORD }}@localhost:5432/${{ env.POSTGRES_DB }}?sslmode=disable" >> $GITHUB_ENV - run: env - name: Make Migrations run: make migrations URL=${{ env.DB_URL }} - name: Seed test DB run: go run db/seed.go - name: Test run: make test
Here we have assigned a name to the job that will perform the core tasks, which are building and testing our code.
Runner - describes where the workflow will run which will is a Ubuntu viritual machine.
Services
Github Actions workflows allow you to define services. In this case we need a postgres database to run our tests against.
- A PostgreSQL container is created using the official PostgreSQL Docker image.
- The container is configured with environment variables we declared earlier
Workflow Steps
- First step is to checkout the repository code
jobs: build: name: tests runs-on: ubuntu-latest
This line fetches the latest version of the repository, providing access to all source files.
- Second step is to setup golang in the runner.
- uses: actions/checkout@v4
- The third step is installing dbmate on our runner. Dbmate is a migration tools that will manage application migrations.
- name: Set up Go uses: actions/setup-go@v4 with: go-version: "1.22"
- Fourth is to construct the db url
- name: Install dbmate for golang migrations run: | sudo curl -fsSL -o /usr/local/bin/dbmate https://github.com/amacneil/dbmate/releases/latest/download/dbmate-linux-amd64 sudo chmod +x /usr/local/bin/dbmate which dbmate
- Fifth is runnig db migrations to setup our relations that will seed with date
- name: Construct DB URL id: construct_url run: echo "DB_URL=postgres://${{ env.POSTGRES_USER }}:${{ env.POSTGRES_PASSWORD }}@localhost:5432/${{ env.POSTGRES_DB }}?sslmode=disable" >> $GITHUB_ENV
- The second last action is to seed the database with test data.
- name: Make Migrations run: make migrations URL=${{ env.DB_URL }}
The seed.go file seeds the data ase with test data. Setting up a realistic test environment. To inspect this file further, visit here
The final stage is to execute our go test using the make file
- name: Seed test DB run: go run db/seed.go
This workflow will now run each time we make a pull request or push code to our main branch
Some Advantages of Adopting Github Action.
As we have seen github action allow you to do
- Automated Testing - run tests consistently on every code change.
- Have Database Integretions - provide a real postgres environment for testing, simulating production conditions
- Reproducible Environment - Github action use containerized services and predefined steps to ensure consistent results across all runs.
- Fast Feedback loop - They enable developers to receive quick feedback if something breaks, allowing for faster issue resolution.
- Simplified Collaboration - They ensure all contributors' changes are verified before emerging, maintaining code quality and project stability
Conclusions
By leveraging GitHub Actions, this workflow streamlines testing and database setup, ensuring robust and reliable software development.
Visit the github repository to view the code that is being tested with the action described above.
The above is the detailed content of Seamless Integration Testing for Your Go Application on GitHub Actions with PostgreSQL. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











Go language performs well in building efficient and scalable systems. Its advantages include: 1. High performance: compiled into machine code, fast running speed; 2. Concurrent programming: simplify multitasking through goroutines and channels; 3. Simplicity: concise syntax, reducing learning and maintenance costs; 4. Cross-platform: supports cross-platform compilation, easy deployment.

Golang is better than Python in terms of performance and scalability. 1) Golang's compilation-type characteristics and efficient concurrency model make it perform well in high concurrency scenarios. 2) Python, as an interpreted language, executes slowly, but can optimize performance through tools such as Cython.

Golang is better than C in concurrency, while C is better than Golang in raw speed. 1) Golang achieves efficient concurrency through goroutine and channel, which is suitable for handling a large number of concurrent tasks. 2)C Through compiler optimization and standard library, it provides high performance close to hardware, suitable for applications that require extreme optimization.

Goimpactsdevelopmentpositivelythroughspeed,efficiency,andsimplicity.1)Speed:Gocompilesquicklyandrunsefficiently,idealforlargeprojects.2)Efficiency:Itscomprehensivestandardlibraryreducesexternaldependencies,enhancingdevelopmentefficiency.3)Simplicity:

Golang and Python each have their own advantages: Golang is suitable for high performance and concurrent programming, while Python is suitable for data science and web development. Golang is known for its concurrency model and efficient performance, while Python is known for its concise syntax and rich library ecosystem.

The performance differences between Golang and C are mainly reflected in memory management, compilation optimization and runtime efficiency. 1) Golang's garbage collection mechanism is convenient but may affect performance, 2) C's manual memory management and compiler optimization are more efficient in recursive computing.

Golang and C each have their own advantages in performance competitions: 1) Golang is suitable for high concurrency and rapid development, and 2) C provides higher performance and fine-grained control. The selection should be based on project requirements and team technology stack.

Golang is suitable for rapid development and concurrent scenarios, and C is suitable for scenarios where extreme performance and low-level control are required. 1) Golang improves performance through garbage collection and concurrency mechanisms, and is suitable for high-concurrency Web service development. 2) C achieves the ultimate performance through manual memory management and compiler optimization, and is suitable for embedded system development.
