Commit 950acbb7 authored by Thomas Boni's avatar Thomas Boni
Browse files

Merge branch '383-new-job-golang-unit-tests' into 'latest'

Resolve "[New job] - {golang-unit-tests}"

Closes #383

See merge request r2devops/hub!209
parents 290c70b2 cdc3b132
Loading
Loading
Loading
Loading
+24 −0
Original line number Diff line number Diff line
## Description

Execute your golang unit tests and have the results fully integrated with the various areas of the Gitlab UI plus provide code coverage data in pipelines and badges.

## How to use it

1. The job of course assumes that you have written your tests 
2. Choose a version in [version list](#changelog)
3. Add the corresponding URL to your `.gitlab-ci.yml` file (see [Getting
   started](/use-the-hub/)). Example:
    ```yaml
    include:
    - remote: 'https://jobs.r2devops.io/go_unit_test.yml'
    ```
4. If you need to customize the job (stage, variables, ...) 👉 check the
   [jobs customization](/use-the-hub/#jobs-customization)
5. Well done, your job is ready to work ! 😀

## Variables

| Name | Description | Default |
| ---- | ----------- | ------- |
| `UT_WORK_DIR` | If for any reason (monorepo for example) your go project is not at root of project, specify the relative path to the project | `$CI_PROJECT_DIR` |
| `UT_TEST_CMD` | Customise the test command if needed | `go test -v` |
+32 −0
Original line number Diff line number Diff line
stages:
    - static_tests

go_unit_test:
  stage: static_tests
  image: golang:1.16.3
  variables:
    UT_WORK_DIR: $CI_PROJECT_DIR
    UT_TEST_CMD: "go test -v"
  script:
    # Switch to working directory
    - cd "$UT_WORK_DIR"
    # Install job dependencies
    - go get -u github.com/jstemmer/go-junit-report@v0.9.1 github.com/t-yuki/gocover-cobertura@v0.0.0-20180217150009-aaee18c8195c
    # Execute Unit tests to get both the junit report and the coverage file
    - ${UT_TEST_CMD} -coverprofile=coverage.out ./... 2>&1 | go-junit-report -set-exit-code > report.xml
    # Output the coverage to stdout for coverage regex to read
    - go tool cover -func=coverage.out
    # Capture coverage in cobertura format for MR highlighting
    - gocover-cobertura < coverage.out > cobertura.xml
    # Capture coverage in HTML format for humans
    - go tool cover -html=coverage.out -o code-coverage.html
  # Regex below tells gitlab what to capture from job's stdout as a coverage figure
  coverage: '/total:\s+\(statements\)\s+(\d+.\d+%)/'
  artifacts:
    paths:
      - report.xml
      - code-coverage.html
    when: always
    reports:
      junit: report.xml
      cobertura: cobertura.xml
+16 −0
Original line number Diff line number Diff line
name: go_unit_test
description: Perform golang unit tests and integrate results with Gitlab UI
default_stage: static_tests
icon: 🧪
maintainer: seanel
license: MIT
images:
  "golang": "1.16.3"
tools:
  "github.com/t-yuki/gocover-cobertura": "v0.0.0-20180217150009-aaee18c8195c"
  "github.com/jstemmer/go-junit-report": "v0.9.1"

labels:
  - GitLab
  - Golang
  - Test
+0 −0

Empty file added.

+4.03 KiB
Loading image diff...
Loading