Home Backend Development Golang Seamless Integration Testing for Your Go Application on GitHub Actions with PostgreSQL

Seamless Integration Testing for Your Go Application on GitHub Actions with PostgreSQL

Nov 28, 2024 pm 09:55 PM

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

Copy after login
Copy after login

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

Copy after login
Copy after login

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
Copy after login

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
Copy after login
  • 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"
Copy after login
  • 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

Copy after login
  • 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

Copy after login
  • The second last action is to seed the database with test data.
- name: Make Migrations
  run: make migrations URL=${{ env.DB_URL }}
Copy after login

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
Copy after login

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

  1. Automated Testing - run tests consistently on every code change.
  2. Have Database Integretions - provide a real postgres environment for testing, simulating production conditions
  3. Reproducible Environment - Github action use containerized services and predefined steps to ensure consistent results across all runs.
  4. Fast Feedback loop - They enable developers to receive quick feedback if something breaks, allowing for faster issue resolution.
  5. 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!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1664
14
PHP Tutorial
1266
29
C# Tutorial
1239
24
Golang's Purpose: Building Efficient and Scalable Systems Golang's Purpose: Building Efficient and Scalable Systems Apr 09, 2025 pm 05:17 PM

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 vs. Python: Performance and Scalability Golang vs. Python: Performance and Scalability Apr 19, 2025 am 12:18 AM

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 and C  : Concurrency vs. Raw Speed Golang and C : Concurrency vs. Raw Speed Apr 21, 2025 am 12:16 AM

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.

Golang's Impact: Speed, Efficiency, and Simplicity Golang's Impact: Speed, Efficiency, and Simplicity Apr 14, 2025 am 12:11 AM

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

Golang vs. Python: Key Differences and Similarities Golang vs. Python: Key Differences and Similarities Apr 17, 2025 am 12:15 AM

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.

Golang and C  : The Trade-offs in Performance Golang and C : The Trade-offs in Performance Apr 17, 2025 am 12:18 AM

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.

The Performance Race: Golang vs. C The Performance Race: Golang vs. C Apr 16, 2025 am 12:07 AM

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 vs. C  : Performance and Speed Comparison Golang vs. C : Performance and Speed Comparison Apr 21, 2025 am 12:13 AM

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.

See all articles