Skip to content

test coverage doesn't "pick-up" some test #479

Closed

Description

@iapicca

I apologize in advance for this issue being incomplete.

I'm experiencing the issue with one of my package and I cannot identify the cause,
I tried to reproduce the behavior with the code sample below, but it behave as intended

failed code sample

set up

sudo apt install lcov
flutter pub global activate coverage
flutter pub global activate stagehand
mkdir coverage_issue && cd coverage_issue
stagehand package-simple

code

  • create src/foo.dart
class Foo {
  const Foo();
  int call(String string) => string.length;
}
  • create src/extension.dart
import 'foo.dart';

extension FooX on String {
  int fooify() {
    final foo = Foo();
    return foo(this);
  }
}

test

  • create test/foo_test.dart
import 'package:coverage_issue/coverage_issue.dart';
import 'package:test/test.dart';

void main() {
  test('foo test', () {
    const string = 'hello';
    final foo = Foo();
    expect(foo(string), string.length);
  });
  test('fooify test', () {
    const string = 'hello';
    expect(string.fooify(), string.length);
  });
}

commands

flutter test --coverage
format_coverage --lcov --check-ignore  --in=coverage --out=coverage.lcov --packages=.packages --report-on=lib
genhtml -o coverage/html coverage/lcov.info

results

Reading data file coverage/lcov.info
Resolved relative source file path "lib/src/foo.dart" with CWD to "/home/yakforward/projects/coverage_issue/lib/src/foo.dart".
Found 2 entries.
Found common filename prefix "/home/yakforward/projects/coverage_issue/lib"
Writing .css and .png files.
Generating output.
Processing file src/extension.dart
Processing file src/foo.dart
Writing directory view page.
Overall coverage rate:
  lines......: 100.0% (5 of 5 lines)
  functions..: no data found

everything seems to work as intended

running the same commands on my package I get the result below

Reading data file coverage/lcov.info
Resolved relative source file path "lib/src/classes/error/handler.dart" with CWD to "/home/yakforward/projects/yak_packages/packages/yak_runner/lib/src/classes/error/handler.dart".
Found 38 entries.
Found common filename prefix "/home/yakforward/projects/yak_packages/packages/yak_runner/lib/src"
Writing .css and .png files.
Generating output.
[...] /// tedious stuff
Writing directory view page.
Overall coverage rate:
  lines......: 96.3% (208 of 216 lines)
  functions..: no data found

looking it up

cd coverage/html
chromium index.html

the (allegedly) not tested lines refer to 3 extensions (for short) parse, encode and decode
which are indeed tested (parse, encode and decode)

I'm aware that as it is the issue is hardly workable,

can you please suggest a way to reproduce the problem?

possibly related to #475

Activity

  1. iapicca commented on May 11, 2021

    @iapicca
    Author

    update

    while somehow the issue disappeared "duplicating" the test for the (dart) package above
    I'm experiencing the same problem with another package (from quite some time)
    even if in a larger package the code is very much self contained and I feel that might
    be consider a useful code sample

    here is the ui-output of the coverage screenshot from the same commands of the partial code sample above

    anyhow
    this is the code

    code (at the time of writing)
    import 'package:flutter/material.dart';
    import 'package:yak_runner/yak_runner.dart';
    
    import '../../yak_widgets.dart';
    
    /// a widget that provides a `loading screen`
    class Initializer extends StatelessWidget {
      /// allows a `const` constuctor
      const Initializer({
        required this.loadingScreen,
        required this.progressState,
        required this.onCompleted,
        required this.onFailed,
        Key key = const ValueKey('Initializer'),
      }) :
            // coverage:ignore-line
            super(key: key);
    
      /// requires a `loadingScreen`
      final FractionalIndexItemBuilder loadingScreen;
    
      /// provides the state of the loading
      final ValueNotifier<Result<double>> progressState;
    
      /// provides the screen when loading is completed
      final WidgetBuilder onCompleted;
    
      /// provides the screen when loading has failed
      final WidgetBuilder onFailed;
    
      @override
      Widget build(BuildContext context) {
        return ValueListenableBuilder<Result<double>>(
          key: ValueKey('ValueListenableBuilder@$key'),
          valueListenable: progressState,
          builder: (context, value, _) {
            return value is Success
                ? (value as Success).data < 1
                    ? loadingScreen(context, (value as Success).data)
                    : onCompleted(context)
                : onFailed(context);
          },
        );
      }
    }
    

    and this is the test

    test (at the time of writing)
    import 'package:flutter/material.dart';
    import 'package:flutter_test/flutter_test.dart';
    import 'package:yak_error_handler/yak_error_handler.dart';
    import 'package:yak_runner/yak_runner.dart';
    import 'package:yak_widgets/yak_widgets.dart';
    
    void main() {
      final progress = ValueNotifier<Result<double>>(Success(.0));
      final loading = UniqueKey();
      final failed = UniqueKey();
      final completed = UniqueKey();
    
      final app = MaterialApp(
        home: Material(
          child: Initializer(
            loadingScreen: (context, progress) => Container(key: loading),
            progressState: progress,
            onCompleted: (context) => Container(key: completed),
            onFailed: (context) => Container(key: failed),
          ),
        ),
      );
      group('`Initializer` test', () {
        testWidgets(
            'WHEN progress is Success & < 1 THEN `loadingScreen` is VISIBLE',
            (tester) async {
          await tester.pumpWidget(app);
    
          expect(
            find.byKey(loading),
            findsOneWidget,
            reason: ' `loadingScreen` should be VISIBLE',
          );
          expect(
            find.byKey(completed),
            findsNothing,
            reason: ' `onCompleted` should NOT be VISIBLE',
          );
          expect(
            find.byKey(failed),
            findsNothing,
            reason: ' `onFailed` should NOT be VISIBLE',
          );
        });
    
        testWidgets(
            'WHEN progress is Success & >= 1 ' 'THEN `onCompleted` is VISIBLE',
            (tester) async {
          await tester.pumpWidget(app);
          progress.value = Success(1);
          await tester.pumpAndSettle(kThemeAnimationDuration);
    
          expect(
            find.byKey(loading),
            findsNothing,
            reason: ' `loadingScreen` should NOT be VISIBLE',
          );
          expect(
            find.byKey(completed),
            findsOneWidget,
            reason: ' `onCompleted` should be VISIBLE',
          );
    
          expect(
            find.byKey(failed),
            findsNothing,
            reason: ' `onFailed` should NOT be VISIBLE',
          );
        });
    
        testWidgets('WHEN progress is `Failure` ' 'THEN `onFailed` is VISIBLE',
            (tester) async {
          await tester.pumpWidget(app);
          progress.value = Failure<double>(ErrorReport());
          await tester.pumpAndSettle(kThemeAnimationDuration);
    
          expect(
            find.byKey(loading),
            findsNothing,
            reason: ' `loadingScreen` should NOT be VISIBLE',
          );
          expect(
            find.byKey(completed),
            findsNothing,
            reason: ' `onCompleted` should NOT be VISIBLE',
          );
    
          expect(
            find.byKey(failed),
            findsOneWidget,
            reason: ' `onFailed` should  be VISIBLE',
          );
        });
      });
    }
    

    those are both uninteresting and my set up works just fine for the other widgets.

    I wish I could be more helpful,
    can you please suggest a way to reproduce the problem?

  2. natebosch commented on May 12, 2021

    @natebosch
    Member

    can you try renaming the test so that it ends in _test.dart?

  3. iapicca commented on May 12, 2021

    @iapicca
    Author

    ... facepalm
    my bad

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions