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.
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
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.
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
Github workflows support global and job-specific environment variables. This variables describe postgres credentials that we will user later in our yaml file.
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.
Github Actions workflows allow you to define services. In this case we need a postgres database to run our tests against.
jobs: build: name: tests runs-on: ubuntu-latest
This line fetches the latest version of the repository, providing access to all source files.
- 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
- 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
As we have seen github action allow you to do
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!