DLCOV - CLI to verify code coverage threshold (CI/CD, git hooks, etc.)

Main features:

- It can be used in shell scripts, github actions, git hooks, etc, to block the app build if the code coverage is less than the value you enter as the minimum threshold.

- Include files in code coverage that Flutter does not natively include, improving the accuracy of code coverage threshold values.

ℹ️ Flutter (test --coverage) doesn't include files that weren't referenced in tests, which can give a false impression of greater code coverage than it actually is, ONLY if you don't take this implicit detail into account.

- Remove unwanted files from code coverage reports using the following filters:

  • By file name suffix
  • By regular expression in the filename
  • By the content/code inside the file

- It keeps a log of the tests in a file, being able to observe if the code coverage increased or decreased with each verification.

- Allows the generation of the lcov.info file by external tools, without direct dependencies to the Flutter SDK or other technologies.


Usage Example

dlcov --coverage=80 --include-untested-files=true --lcov-gen="flutter test --coverage" --log=true

The command above does:

  • --coverage=80 Check if code coverage threshold is equal or greater than 80.0%
  • --include-untested-files=true Generate references to untested files (defalt equals "false")
  • --lcov-gen="flutter test --coverage" Generate lcov.info through the command "flutter test --coverage", it can be used with "test_cov" for dart only for example, or others if you prefer.
  • --log=true Log every test coverage info in dlcov.log - Limit up to 1000 lines

Install

pub global activate dlcov and later dlcov -c 80, pub global run dlcov -c 80
or add as dev dependency to pubspec.yaml

dev_dependencies:
  dlcov: 4.2.0

# pub run dlcov -c 80

Verify if code coverage threshold is equal or greater than 80.0%

dlcov gen-refs && flutter test --coverage && dlcov -c 80
dlcov -c 80 --lcov-gen="flutter test --coverage" --include-untested-files=true
dlcov -c 80 --lcov-gen="test_cov" --include-untested-files=true


Parameters availables

LongShortMandatoryDefaultSampleDescription
--coverage-cfalse080.0min coverage threshold
--log-lfalsefalsetrueLog every test coverage info in dlcov.log - Limit up to 1000 lines
--include-untested-filesfalsefalsetrueGet reports more coherent with reality, and do not ignore untested files during the analysis
--lcov-genfalse"flutter test --coverage"Generate lcov.info through the command "flutter test --coverage", it can be used with "test_cov" for dart only for example, or others if you prefer
--exclude-suffix-efalse.g.dart,.freezed.dart.g.dartExclude files from test coverage results, using suffixes, separated by commas
--exclude-filesfalse"*widget\.dart,*_part_*"Exclude all files from test coverage results, with path names that contains "_part_" and path names that ends with "widget.dart"
--exclude-contentsfalse"class*extends StatefulWidget*"Exclude all files from test coverage results if those files contains this patterns. In the example it exclude all files that contains classes that extends StatefulWidget
--exclude-contents-pathfalse"./patterns_list.txt"Path to file that contains all patterns that will be used to exclude all files from test coverage results (This overrides --exclude-contents)

Examples how to removing/ignoring files from code coverage reports:
dlcov --exclude-contents-path=example/patterns.txt -c 80 --lcov-gen="flutter test --coverage" --include-untested-files=true

dlcov --exclude-contents="class*extends StatefulWidget*" -c 80 --lcov-gen="flutter test --coverage" --include-untested-files=true

dlcov --exclude-files="*\.g\.dart" -c 80 --lcov-gen="flutter test --coverage" --include-untested-files=true

dlcov --exclude-suffix=".g.dart" -c 80 --lcov-gen="flutter test --coverage" --include-untested-files=true


CommandDescription
gen-refsIt generate test file references for all dart files under lib folder, it should be used before lcov.info file generation step. And it can be filtered, using additional parameters.
Examples:
dlcov gen-refs --exclude-contents-path=example/patterns.txt
dlcov gen-refs --exclude-files="lib/usecases/*,*/models/*,*\.g\.dart" all files under "lib/usecases/" folder, all files with "models" in the path, all files that contains ".g.dart"

Github actions

if the test coverage is less than 80, it stop the pipeline here, and abort the actions

Screen Shot 2022-03-20 at 01 49 51

Libraries

app_command
app_constants
app_error_codes
config
config.a
config_model
config_repository
coverage
create_file_references
delete_file_references
file_matcher_util
file_system_util
gen_refs_command
get_config
get_lcov
get_records
lcov
list_extension
logger
parse_arguments
process_util
record_repository
verify_coverage