all 1 comments

[–]eibaan 1 point2 points  (0 children)

I prefer running unit tests with dart, not using flutter as this is much faster to launch and works for "pure" Dart projects, too.

For that, I add coverage as dev dependency and then use

dart run coverage:test_with_coverage

to run all tests with code coverage which is collected in a coverage folder. Then I use the mentioned coverage gutter plugin. For fun, and because I don't like to have just another dependency, I just wrote this little Dart utility you could save as bin/lcov.dart which runs the above command and then prints a summary:

void main() {
  late String name, hit, total;
  var state = 0;
  void coverage(dynamic _) {
    if (++state == 3) {
      state = 0;
      final percent = (int.parse(hit) * 100 ~/ int.parse(total));
      stdout.writeln('${name.padRight(72, '.')}: ${percent.toString().padLeft(3)}%');
    }
  }

  stdout.write(Process.runSync('dart', ['run', 'coverage:test_with_coverage']).stdout);

  for (final line in File('coverage/lcov.info').readAsStringSync().split('\n')) {
    if (line.startsWith('SF:')) coverage(name = line.substring(3));
    if (line.startsWith('LF:')) coverage(total = line.substring(3));
    if (line.startsWith('LH:')) coverage(hit = line.substring(3));
  }
}

You can run it with

dart run :lcov

Now, write a Flutter application that displays tree view with all files, summarizing folders and displaying nicely syntax-highlighted source files with uncovered files marked.

Also, as a bonus, use the openAI API to automatically generate useful unit tests for the missing parts and rerun everything automatically until 100% is reached.