DEV Community

Krisada Vivek
Krisada Vivek

Posted on • Edited on

Testing on Flutter: generate unit test badge using LCOV to your repository

TLDR; in this publication, I assumed that you all know how to write test and testing terminology and methods, no mentions here.

What is LCOV

LCOV is a graphical front-end for GCC’s coverage testing tool gcov. It collects gcov data for multiple source files and creates HTML pages containing the source code annotated with coverage information. It also adds overview pages for easy navigation within the file structure. LCOV supports statement, function, and branch coverage measurement.

Installing LCOV

brew install lcov
Enter fullscreen mode Exit fullscreen mode
  • Generating an example app — flutter counter app

  • example test scripts ./test/widget_test.dart

Objective of lcov

  • To see the coverage of the code.
  • To deliver the code with quality.

To perform the test with coverage

install test_coverage library in pubspec.yml under dev_dependencies

dev_dependencies:
    flutter_test:
       sdk: flutter
    test_coverage: ^0.4.2
Enter fullscreen mode Exit fullscreen mode
  • then run test command with flags
flutter test --coverage
Enter fullscreen mode Exit fullscreen mode
  • when test complete it will generate folder ‘coverage” in the project directory, to see the result in visualized, run this commands
genhtml -o coverage coverage/lcov.info
# Open in the default browser (mac):
open coverage/index.html
this is the html report, that generated out looks like.
Enter fullscreen mode Exit fullscreen mode

Alt Text

  • now you will get the coverage result, but I want to present in the GitHub repo how do I do that? you should get a badge!!!

To generate badge image

  • assume that you are at the root of the project
$ yarn init
$ yarn add lcov2badge — more detail here
Enter fullscreen mode Exit fullscreen mode
  • then create a javascript script to generate a badge form the coverage file.
  • what you get is the ./coverage_badge.svg that you can use inside the README.md file, like this.

Alt Text

![Coverage](./coverage_badge.svg?sanitize=true)
Enter fullscreen mode Exit fullscreen mode
  • the example Github code can be found here!

GitHub logo 3lVv0w / test_lcov_bedge

this repo demonstrate how to generate coverage badge form lcov.info for unit testing




If you have any question/discussion with me, you can contact/ follow me
FB: Krisada Vivek
DEV.to: @3lvv0w

Follow me Krisada Vivek for a more upcoming tech publication

ThankYou! ❤

Top comments (0)