Flutter: Inline Test Coverage in Android Studio

前端 未结 2 1282
一向
一向 2021-01-19 01:25

I\'m developing an open source Android app in Flutter, using Android Studio 3.3.1 (on Window and Ubuntu). The source is available on Github.

The project has a test f

2条回答
  •  甜味超标
    2021-01-19 01:52

    I don't think is supported for Flutter projects yet.

    I have all non-UI code in another pure Dart package that I add as dependency to the Flutter project.

    For my project this also has the advantage that code that I can share with the browser GUI (Angular Dart) is separated and can't accidentally be polluted with Flutter dependencies that would break the web project.

    In this project I can get coverage information in IntellJ when I follow these steps:

    You need a "Dart Command Line App" IntelliJ run configuration instead of a "Dart Test", "Flutter" or "Flutter Test" run configuration.

    To be able to run tests with a "Dart Command Line App" run configuration you probably need the standalone Dart SDK installed and select it in Preferences > Languages & Frameworks > Dart > Dart SDK path.

    To run all tests with coverage instead of individual files you need a file like

    test/all.dart

    // ignore_for_file: await_only_futures
    
    import 'dart:async';
    
    import 'client/controller/app_controller_test.dart' as i0;
    import 'client/controller/authentication_controller_test.dart' as i1;
    import 'client/controller/backoffice/backoffice_controller_test.dart' as i2;
    import 'client/controller/backoffice/image_reference_controller_test.dart'
        as i3;
    ...
    
    Future main() async {
      i0.main();
      i1.main();
      i2.main();
    ...
    } 
    

    with an entry for each test file.

    I use a Grinder task like below to generate that file automatically

    import 'package:path/path.dart' as path;
    ...
    /// Generate a single Dart file that executes all tests.
    /// Dart code coverage reporting still requires that.
    @Task('generate test/all.dart')
    Future prepareCoverage() async {
      final testDir = Directory('test');
      final context = path.Context(style: path.Style.posix);
      final testFiles = testDir
          .listSync(recursive: true, followLinks: false)
          .where((e) =>
              FileSystemEntity.isFileSync(e.path) && e.path.endsWith('_test.dart'))
          .map(
              (tf) => context.normalize(path.relative(tf.path, from: testDir.path)))
          .toList()
            ..sort();
      final content = StringBuffer('''
    // ignore_for_file: await_only_futures
    
    import 'dart:async';
    
    ''');
      final executions = StringBuffer();
      for (var i = 0; i < testFiles.length; i++) {
        final testFile = testFiles[i];
        content.writeln("import '$testFile' as i$i;");
        executions.writeln('  i$i.main();');
      }
      content
        ..writeln('Future main() async {')
        ..writeln()
        ..writeln(executions)
        ..writeln('}');
      File('test/all.dart').writeAsStringSync(content.toString());
      PubApp.global('dart_style')
          .run(['-w', '--fix']..add('test/all.dart'), script: 'format');
    }
    

提交回复
热议问题