Skip to content

Fix Integration Test section of 'How to test a Flutter app' page #8949

Open
@LokieVikky

Description

@LokieVikky

Page URL

https://codelabs.developers.google.com/codelabs/flutter-app-testing#6

Page source

No response

Describe the problem

"How to Test a Flutter App" Code lab has a problem in the Integration Test Section

import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:testing_app/main.dart';

void main() {
  group('Testing App', () {
    testWidgets('Favorites operations test', (tester) async {
      await tester.pumpWidget(const TestingApp());

      final iconKeys = [
        'icon_0',
        'icon_1',
        'icon_2',
      ];

      for (var icon in iconKeys) {
        await tester.tap(find.byKey(ValueKey(icon)));
        await tester.pumpAndSettle(const Duration(seconds: 1));

        expect(find.text('Added to favorites.'), findsOneWidget);
      }

      await tester.tap(find.text('Favorites'));
      await tester.pumpAndSettle();

      final removeIconKeys = [
        'remove_icon_0',
        'remove_icon_1',
        'remove_icon_2',
      ];

      for (final iconKey in removeIconKeys) {
        await tester.tap(find.byKey(ValueKey(iconKey)));
        await tester.pumpAndSettle(const Duration(seconds: 1));

        expect(find.text('Removed from favorites.'), findsOneWidget);
      }
    });
  });
}

This Test will fail because we are adding three favourites by finding the icon keys 1 to 3
and we are trying to remove the added favourites on another page using 3 keys which are

      final removeIconKeys = [
        'remove_icon_0',
        'remove_icon_1',
        'remove_icon_2',
      ];

Since the page is rebuilding for every remove action
Indexes before performing the remove action: 0, 1, 2.
Indexes after first remove action: 0, 1.
Indexes after second remove action: 0.

So when trying to find the widget using the value key 'remove_key_2', the test will fail because that left the list after removing the first item itself.

Expected fix

Replace

      final removeIconKeys = [
        'remove_icon_0',
        'remove_icon_1',
        'remove_icon_2',
      ];

with

      final removeIconKeys = [
        'remove_icon_0',
        'remove_icon_0',
        'remove_icon_0',
      ];

Additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    a.tut.codelabRelates to codelab hosted on docs.flutter.deve1-hoursEffort: < 8 hrsfix.examplesAdds or changes examplelang.dartInvolves Dart codep2-mediumNecessary but not urgent concern. Resolve when possible.test.integrationRelates to integration testing

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions