Home Arrow Icon Knowledge base Arrow Icon Global Arrow Icon How do I test a Go application on CircleCI


How do I test a Go application on CircleCI


To test a Go application on CircleCI, you can follow these steps:

1. Create a Go Project

1. Create a Go Project: Start by creating a Go project. This can be done using a tool like `go mod init` to initialize a new Go module.

2. Set Up CircleCI

1. Set Up CircleCI: Sign up for a CircleCI account and link your GitHub repository to CircleCI. This will allow CircleCI to automatically detect changes to your code and trigger builds.

3. Configure CircleCI

1. Configure CircleCI: Create a `config.yml` file in the `.circleci` directory of your project. This file defines the build and test process for your project.

4. Define Jobs

1. Define Jobs: In the `config.yml` file, define a job that includes the steps to build and test your Go application. This can include commands like `go build`, `go test`, and `go run`.

5. Use the Go Orb

1. Use the Go Orb: CircleCI provides a Go orb that includes prepackaged configurations for common Go-related tasks. This orb can be used to simplify the build and test process.

6. Run Tests

1. Run Tests: Use the `go test` command to run your tests. You can also use the `gotestsum` wrapper to run tests with additional options like coverage and race detection.

Example Configuration

Here is an example `config.yml` file that demonstrates the integration of a Go project with CircleCI:

yaml
version: 2.1

orbs:
  go: circleci/
 <script language='JavaScript' type='text/javascript'>
 <!--
 var prefix = 'm&#97;&#105;lt&#111;:';
 var suffix = '';
 var attribs = '';
 var path = 'hr' + 'ef' + '=';
 var addy26698 = 'g&#111;' + '&#64;';
 addy26698 = addy26698 + '1' + '&#46;' + '11';
 document.write( '<a ' + path + '\'' + prefix + addy26698 + suffix + '\'' + attribs + '>' );
 document.write( addy26698 );
 document.write( '<\/a>' );
 //-->
 </script><script language='JavaScript' type='text/javascript'>
 <!--
 document.write( '<span style=\'display: none;\'>' );
 //-->
 </script>This e-mail address is being protected from spambots. You need JavaScript enabled to view it
 <script language='JavaScript' type='text/javascript'>
 <!--
 document.write( '</' );
 document.write( 'span>' );
 //-->
 </script>.0

jobs:
  build_and_test:
    executor:
      name: go/default
      tag: "1.22"
    steps:
      - checkout
      - go/load-cache
      - go/mod-download
      - go/save-cache
      - go/test:
          covermode: atomic
          failfast: true
          race: true

workflows:
  main:
    jobs:
      - build_and_test

This configuration defines a job that checks out the code, loads and saves Go modules, and runs tests with coverage and race detection. The workflow defines a single job that runs the `build_and_test` job.

Running Tests Locally

To run tests locally, you can use the `go test` command. For example:
bash
go test

To disable Gin debug logs and enable verbose mode, you can use the `-V` flag:
bash
GIN_MODE=release go test -v

Automating Tests

To automate your tests, you can create a continuous integration pipeline on CircleCI. This involves creating a `config.yml` file in the `.circleci` directory of your project. Here is an example `config.yml` file that includes the Go orb and runs tests:

yaml
version: "2.1"

orbs:
  go: circleci/
 <script language='JavaScript' type='text/javascript'>
 <!--
 var prefix = 'm&#97;&#105;lt&#111;:';
 var suffix = '';
 var attribs = '';
 var path = 'hr' + 'ef' + '=';
 var addy50459 = 'g&#111;' + '&#64;';
 addy50459 = addy50459 + '1' + '&#46;' + '11';
 document.write( '<a ' + path + '\'' + prefix + addy50459 + suffix + '\'' + attribs + '>' );
 document.write( addy50459 );
 document.write( '<\/a>' );
 //-->
 </script><script language='JavaScript' type='text/javascript'>
 <!--
 document.write( '<span style=\'display: none;\'>' );
 //-->
 </script>This e-mail address is being protected from spambots. You need JavaScript enabled to view it
 <script language='JavaScript' type='text/javascript'>
 <!--
 document.write( '</' );
 document.write( 'span>' );
 //-->
 </script>.0

jobs:
  build:
    executor:
      name: go/default
      tag: "1.22"
    steps:
      - checkout
      - go/load-cache
      - go/mod-download
      - go/save-cache
      - go/test:
          covermode: atomic
          failfast: true
          race: true

workflows:
  main:
    jobs:
      - build

This configuration defines a job that checks out the code, loads and saves Go modules, and runs tests with coverage and race detection. The workflow defines a single job that runs the `build` job.

Citations:
[1] https://circleci.com/docs/language-go/
[2] https://circleci.com/developer/orbs/orb/circleci/go
[3] https://circleci.com/blog/continuous-deployment-for-go-applications/
[4] https://github.com/CircleCI-Public/circleci-demo-go
[5] https://circleci.com/blog/gin-gonic-testing/