Coverage
assay reads line coverage from the lcov report that your own tests wrote. It runs no test.
assay metrics --coverage <file>shows it in the metric report and judges nothing: see In the metric report.assay gate --coverage <file>judges the gate conditions underassay.gate.coverage, in CI: see In the gate.
A report is read only when you name it with --coverage. The gate at turn end names none, so it never judges coverage, and an old report in coverage/ is never read by surprise. Only the DA lines count: function and branch records (FN, FNDA, BRDA and their totals) are skipped, and LF and LH are recounted.
Writing the report
Section titled “Writing the report”Write it after the checkout, in the package root, with one of:
| Command | Where | How it names a file | Honours coverage-ignore comments |
|---|---|---|---|
flutter test --coverage |
coverage/lcov.info (--coverage-path to change) |
relative to the package root: lib/src/a.dart (backslashes on Windows) |
yes |
dart test --coverage-path=coverage/lcov.info |
the path given | absolute | yes |
dart run coverage:test_with_coverage |
coverage/lcov.info |
absolute | yes |
dart run coverage:format_coverage --lcov … |
the path given | absolute, or relative to --base-directory |
only with --check-ignore |
dart test --coverage=<directory> writes JSON, not lcov; turn it into a report with format_coverage --lcov --check-ignore. Give --base-directory the package root or nothing: with -b lib the report names files that are not there, and is refused. Paths with symbolic links resolved (/private/var/… on macOS) and file: or package: addresses are read too. A report merged from several machines is read as one, as long as its paths name the project’s files here.
Which files are measured
Section titled “Which files are measured”Every Dart file under lib/ that assay judges. Records of other files are left out and counted: tests, files under bin/, paths assay.exclude leaves out (by default, the generated files), and files outside the project, such as another package of a pub workspace (../other/lib/a.dart).
Files the report does not list
Section titled “Files the report does not list”flutter test --coverage and dart test list only the libraries some test loaded. assay counts a file with no record as uncovered: none of its lines ran. It counts its coverable lines itself, from the syntax tree, as the Dart VM would.
Which lines assay counts as coverable
Coverable:
- the first line of each function, method, getter, setter, operator and constructor with code (annotations included, doc comment not), external ones too, but no abstract one, redirecting factory constructor, or constructor of an enum or extension type;
- each top-level or static variable whose initializer is more than a number, a plain string, a boolean or
null; - each line of code with a call, an operator that is a call (not
&&,||,??,!or a comparison withnull), a read through a dot or an index, a bare-name read or write of a member ofthis, a non-constant list or map, a closure, a string interpolation,await, a type test (is, a pattern that tests a value,on SomeType), anassertcondition, or theforof a loop over elements.
Never coverable: class, mixin, enum and extension declarations, instance field initializers (but a closure in one), the initializer of a late field or a late local, constants, a return, yield, throw, break or continue of a bare name or literal, local variable assignments, casts, braces, comments and empty lines.
Coverage-ignore comments
Section titled “Coverage-ignore comments”A coverage-ignore comment takes lines out of the count:
log(message); // coverage:ignore-line// coverage:ignore-startvoid debugDump() { … }// coverage:ignore-end// coverage:ignore-fileThe grammar is the coverage package’s: //, any spaces, coverage:ignore- and line, start, end or file, then only letters, digits, underscores and spaces. So // coverage:ignore-line because it only logs works; // coverage:ignore-line, because, // coverage: ignore-line and block comments do not. The text counts inside a string too: the package reads lines, not comments.
- A region takes out its two comment lines and everything between;
ignore-filethe whole file, wherever it stands. An unbalanced start or end takes out nothing (and thecoveragepackage writes no report for such a file). - assay applies the comments itself to every measured file, so the figure does not depend on the program that wrote the report. Lines taken out are counted as ignored.
- A comment that a change adds is listed by the gate, like a new suppression, and fails nothing: see Coverage-ignore comments at the gate.
Stale reports
Section titled “Stale reports”A measured file is stale when it was modified after the report, or when the report counts a line past its end. A condition that would count a stale file is not judged, rather than failed. On CI, run the tests (or download the report) after the checkout: a report restored before it is older than every file. A step that rewrites files under lib/ after the tests, such as flutter pub get generating the localization classes again, makes the report older than them too: run that step before the tests, or the tests last, right before the gate.
A record of a file that is gone (deleted or moved since) leaves min_overall not judged, where it is under lib/ and not excluded: “the coverage report lists lib/old.dart, which is no longer there”. A note names each such file and gone counts them in JSON. A moved file keeps its modification time, so it is not stale at its new path.
Reports that are refused
Section titled “Reports that are refused”A command ends with a usage error (exit code 2, no result) when the file named by --coverage:
- does not exist or cannot be read;
- is not lcov: the message names the first bad line, such as a
DAline without a line number and a count, or a record withoutend_of_record(a report cut short); - holds no record;
- names no file of the project that is there: a report from another machine names the paths there, and one written with another base directory (
format_coverage --lcov -b lib) names them from that directory. The message names the first record.
In the metric report
Section titled “In the metric report”assay metrics --coverage coverage/lcov.info shows the coverage of the files it measures under lib/ (of the given paths, if any): the total share and lines, the files not in the report, the stale ones, the ignored lines, the listed files that are not measured, and each file’s share (the ten lowest in the console, every file in JSON and HTML). It exits with 0 whatever the figure.
╭─ Line coverage ──────────────────────────────────────────── 40.0% · 3 files ─╮│ 6 of 15 coverable lines ran, by the report coverage/lcov.info ││ 1 file not in the report, counted as not run ││ Listed in the report and not measured: 1 file of the project ││ ││ Coverage Lines File ││ ───────────────────────────────────────────────────────────── ││ 0.0% 0/5 lib/feature/unlisted.dart not in the report ││ 40.0% 2/5 lib/feature/half.dart ││ 80.0% 4/5 lib/listed.dart │╰──────────────────────────────────────────────────────────────────────────────╯The console, JSON and HTML forms show the same numbers; see Line coverage for each.
In the gate
Section titled “In the gate”assay gate --coverage coverage/lcov.info judges the conditions that the configuration writes:
assay: gate: coverage: min_on_new_lines: 80 # percent of the new coverable lines of a change min_overall: 70 # percent of all coverable lines small_change: 20 # a change of fewer new coverable lines is not judged on themNo preset writes a condition, and none is judged unless an options file writes it (Gate conditions). A threshold is a percent from 0 to 100, whole or not.
min_overallcounts every measured file, in both judgements.min_on_new_linescounts the new coverable lines of the changed files underlib/. Only the judgement of a change judges it; the project judgement lists it as not judged.small_change: a change with fewer new coverable lines than this (20 unless written) is not judged onmin_on_new_lines.0judges every change with a new coverable line.
A new line is a line of a changed file that its before-state (the commit --base names, a mark’s commit, or the list of changes beside --since) does not hold, by a line diff.
- Every line is new in a new file, a file git does not track (one moved without git among them) and a file whose before-state is unknown. A new file no test loaded counts every coverable line as not run.
- A file renamed in git (by
git mvor in a commit since the base) is compared with its old file, even if also edited, so a file that only moved has no new line. git takes two files for a rename when they are at least half alike. A list of changes records no rename.
Rounding never decides. A condition passes when covered lines × 100 ≥ threshold × coverable lines: 699 of 1000 lines (69.9%) fail a threshold of 70, and 700 pass it. Shares are shown rounded down to tenths, so a failed condition never shows its threshold reached: 1599 of 2000 lines show 79.9%, and only a fully covered file shows 100.0%. A condition’s share keeps as many decimals as its threshold has: 8026 of 10,000 lines show 80.26% beside a threshold of 80.25.
Not judged. A condition is listed as not judged, and fails nothing, when:
- no report was given (so at turn end, coverage never fails the gate);
- the report is older than a file the condition counts (Stale reports); the reason names the files;
- for
min_overall, the report lists a file underlib/that is gone; - the files it counts have no coverable line, or the change is a small change.
A failed condition fails the gate with exit code 1 and gate_condition in failedBecause. Under a configuration error nothing is judged and no report is read.
The console report and the full report end with a box of the conditions and the line coverage, above the verdict box. The Next: row of the verdict says how far to raise the coverage, and runs the gate again with the same report:
╭─ Gate conditions ────────────────────────────────────── 1 failed · 1 passed ─╮│ Condition Share Threshold Status ││ ─────────────────────────────────────────────────── ││ coverage on the new lines 62.5% least 80% failed ││ coverage overall 72.0% least 70% passed ││ ││ Line coverage 72 of 100 coverable lines ran (72.0%), 10 of 16 new ones ││ (62.5%), by the report coverage/lcov.info │╰──────────────────────────────────────────────────────────────────────────────╯
╭─ ✗ FAIL ─────────────────────────────────────────────────────────────────────╮│ 0 blocking · 0 advisory · 0 of 1 file ││ → Next: raise the coverage of the new lines to 80% (3 more lines covered), ││ then run `assay gate --scope changed --coverage coverage/lcov.info` again. │╰──────────────────────────────────────────────────────────────────────────────╯A condition not judged has its reason in the row below it. Notes below the boxes name the unlisted, stale, gone and unmeasured files.
In JSON, every gate result holds conditions[], one entry per condition written: metric (coverage), on (new_lines or overall), least (the threshold), most (null here; a ceiling on duplicated code holds it), value (the shown share, or null), status (passed, failed or not_judged) and why (the reason, or null). With --coverage it also holds coverage, as metrics.coverage is written, with newLines (the new coverable lines and those that ran) and, per changed file, newCoverableLines and newCoveredLines.
In CI, --reporter github writes each failed condition as an error titled gate condition, and --reporter markdown:<file> lists the conditions; SARIF and GitLab carry none. A pull request job judges min_on_new_lines against its merge-base: see the pull request job.