Skip to content

Instantly share code, notes, and snippets.

@DeyvidJLira
Last active November 8, 2025 10:03
Show Gist options
  • Select an option

  • Save DeyvidJLira/38aacdd18b86781896ff945bc965b42b to your computer and use it in GitHub Desktop.

Select an option

Save DeyvidJLira/38aacdd18b86781896ff945bc965b42b to your computer and use it in GitHub Desktop.
Coverage Tools for Flutter

In the automation process, you can use these two files.

Example in github actions:

# ... your before steps ...

- name: Run tests with coverage
  run: flutter test --coverage

- name: Filter coverage
  run: dart tool/coverage_filter.dart

- name: Check coverage threshold
  run: dart tool/coverage_check.dart
// ignore_for_file: avoid_print
import 'dart:io';
void main(List<String> args) async {
const minCoverage = 80;
final file = File('coverage/lcov.info');
if (!file.existsSync()) {
stderr.writeln('❌ Coverage file not found.');
exit(1);
}
final content = await file.readAsString();
final totalLines = RegExp(r'^DA:', multiLine: true).allMatches(content).length;
final coveredLines = RegExp(r'^DA:\d+,[1-9]\d*', multiLine: true).allMatches(content).length;
final coverage = (coveredLines / totalLines) * 100;
print('πŸ“Š Coverage: ${coverage.toStringAsFixed(2)}%');
if (coverage < minCoverage) {
stderr.writeln('❌ Coverage below minimum of $minCoverage%. Build failed.');
exit(1);
}
print('βœ… Coverage meets minimum requirement of $minCoverage%.');
}
// ignore_for_file: avoid_print
import 'dart:io';
void main() async {
final lcovFile = File('coverage/lcov.info');
if (!lcovFile.existsSync()) {
print('❌ coverage/lcov.info not found');
exit(1);
}
final lines = await lcovFile.readAsLines();
final filteredLines = <String>[];
bool skipSection = false;
for (final line in lines) {
if (line.startsWith('SF:')) {
skipSection =
line.endsWith('.g.dart') ||
line.endsWith('.freezed.dart') ||
line.endsWith('.mocks.dart') ||
line.endsWith('main.dart') ||
line.contains('coverage:ignore-file');
if (!skipSection) {
filteredLines.add(line);
}
} else if (line == 'end_of_record') {
if (!skipSection) {
filteredLines.add(line);
}
skipSection = false;
} else if (!skipSection) {
filteredLines.add(line);
}
}
await lcovFile.writeAsString(filteredLines.join('\n'));
print('βœ… Coverage filtered successfully');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment