Skip to content

Tests

Tests are very important part of software development. Your task is now to add linter and unit tests to CI/CD pipeline.

Unit tests are already present in the repository.

Template:

image: <image>

before_script:
  - <items>

stages:
  - <items>

<testname>:<frontend/backend>:
  stage: <name>
  script:
    - <items>
  strategy: depend

Frontend

File should be named .gitlab-ci-frontend-tests.yml and included in the main Gitlab CI file .gitlab-ci.yml

Image should be node:14.4.0-alpine

Before script: - Change working dir to frontend or backend - Install npm packages if needed

Linter

Frontend uses React language You can use npx eslint . || true to linter the code

  • Name test as lint:frontend
  • Use stage: test
  • Use node:14.4.0-alpine container
  • Use npx to linter the code
  • Store artifacts
Help

File: frontend/.gitlab-ci-frontend-test.yml

...
lint:frontend:
  stage: test
  script:
    - npx eslint . || true

Unit tests

  • Name test as test:frontend
  • Use stage: test
  • Use npx jest --ci --reporters=default --reporters=jest-junit --coverage command to run unit tests
  • Store artifacts
    artifacts:
      reports:
        junit: frontend/junit.xml
    

Help

File frontend/.gitlab-ci-frontend-test.yml

test:frontend:
  stage: test
  script:
    - npx jest --ci --reporters=default --reporters=jest-junit --coverage
  artifacts:
    reports:
      junit: frontend/junit.xml
  coverage: /All files\s*\|\s*([\d\.]+)/

Backend

Golang needs build variables in order to work: - CGO_ENABLED: 0 - GOOS: linux

Linter

Backend uses Golang language. \ You can use golint package to linter the code

  • Name test as lint:backend
  • Use stage: test
  • Use golang:1.14.4-alpine container
  • Install golint package
  • Use golint -set_exit_status to lint your code and exit if lint suggestions were found

Help

File: backend/.gitlab-ci-backend-test.yml

lint:backend:
    stage: test
    script:
      - go get golang.org/x/lint/golint
      - golint -set_exit_status

Unit tests

  • Name test as test:backend
  • Use stage: test
  • Use same golang container image as before
  • Use go-junit-report package to run unit tests
  • Save result to junit.xml file
  • Store artifacts
    artifacts:
      reports:
        junit: backend/junit.xml
    

Help

File: backend/.gitlab-ci-backend-test.yml

test:backend:
  stage: test
  script:
    - go get github.com/jstemmer/go-junit-report
    - exec 3>&1; go test -v -cover 2>&1 >&3 | go-junit-report > junit.xml
  artifacts:
    reports:
      junit: backend/junit.xml
  coverage: /^coverage:\s(\d+(?:\.\d+)?%)/

Run pipeline

Run your pipeline and fix any errors tests show!


Answer

Backend:

image: golang:1.14.4-alpine

before_script:
  - cd backend

variables:
  CGO_ENABLED: 0
  GOOS: linux

stages:
  - test

lint:backend:
  stage: test
  script:
    - go get golang.org/x/lint/golint
    - golint

test:backend:
  stage: test
  script:
    - go get github.com/jstemmer/go-junit-report
    - exec 3>&1; go test -v -cover 2>&1 >&3 | go-junit-report > junit.xml
  artifacts:
    reports:
      junit: backend/junit.xml
  coverage: /^coverage:\s(\d+(?:\.\d+)?%)/

Frontend:

image: node:14.4.0-alpine

before_script:
  - cd frontend
  - npm install

stages:
  - test

lint:frontend:
  stage: test
  script:
    - npx eslint . || true

test:frontend:
  stage: test
  script:
    - npx jest --ci --reporters=default --reporters=jest-junit --coverage
  artifacts:
    reports:
      junit: frontend/junit.xml
  coverage: /All files\s*\|\s*([\d\.]+)/