Igor's corner

Running flutter based application tests in gitlab pipelines

Published on

Tests results in gitlab pipeline result

Lets save you and future me 5 minutes of precious time as of how to parse the dart tests and coverage results in gitlab pipeline.

tl;dr

In your .gitlab-ci.yml

image: instrumentisto/flutter:latest
stages:
  - test
test:
  stage: test
  before_script:
    - export PATH="$PATH":"$HOME/.pub-cache/bin"
    - flutter pub get
    - flutter clean
    - flutter pub global activate junitreport
    - flutter pub global activate cobertura
  script:
    - flutter test --coverage --machine | tojunit -o report.xml
    - genhtml coverage/lcov.info --output=coverage
    - cobertura convert
  coverage: '/^\s*lines\.+: (\d+\.\d+)% .*/'
  artifacts:
    when: always
    paths:
      - report.xml
      - coverage/
    reports:
      junit:
        - report.xml
      coverage_report:
        coverage_format: cobertura
        path: coverage/cobertura.xml
    expire_in: 1 week

A bit of explanation

- export PATH="$PATH":"$HOME/.pub-cache/bin" - Is needed to make packages we install globally callable by the script

- flutter pub get - You likely need your stuff to run tests don’t you?

- flutter pub global activate junitreport - A package that converts test results output of dart to a junit format which gitlab understands

- flutter pub global activate cobertura - A package that converts coverage report from lcov format used by dart into cobertura.xml that gitlab understands.

| tojunit -o report.xml - tests conversion, the resulting path will be %project_root%/report.xml, feel free to change to your liking, but you’ll also need to adjust the artifacts section of the step accordingly.

- genhtml coverage/lcov.info --output=coverage - Conversion dart coverage result into an xml report. By default, dart test suit outputs it to the coverage/lcov.info. This command converts lcov.info into xml parsable by the cobertura. You’ll need lcov OS package to be installed for this to work, I use image instrumentisto/flutter with it being included.

cobertura convert - Gives us a final result. Cobertura package will generate a file called cobertura.xml in a directory you tell it, in this case its %project_root/coverage/.

coverage: '/^\s*lines\.+: (\d+\.\d+)% .*/' - Regexp to parse the coverage percentage which will be shown as your pipeline result and in you repo stats. (Se pictures below, just don’t look at the number. Really, don’t🫠) Coverage percent in a job Coverage in the repo

In the artifacts part we are telling gitlab where to find the reports and what each of them mean. Adjust the paths here if you changed them previously.

As a result you can see individual test results, the execution time in the piepline, and a coverage in the MR diff and the pipeline.