Unit Testing With Flutter: Getting Started | raywenderlich.com

In this Unit Testing with Flutter tutorial, you’ll improve your programming skills by learning how to add unit tests to your apps.


This is a companion discussion topic for the original entry at https://www.raywenderlich.com/6926998-unit-testing-with-flutter-getting-started

Thanks lowery for writing this tutorial. can you write another advanced Testing tutorial?

1 Like

Hi Hashem!

Thank you for writing in and expressed your interest to learn more about Flutter Testing. I am almost done with Flutter Widget Testing which should be released in a few weeks time!

If you have specific topics of interests to you, please list it down so that I can try to accommodate them in my next tutorial :slight_smile:

Cheers

Looking forward to the new tutorial as well.

1 Like

Looking forward to hear all your feedbacks after publishing it as well :slight_smile:

Hello @lawrey, I am hoping I can get some guidance on what /i might be doing wrong when I try to test a widget in my App.

I am just getting started with unit testing in Flutter, and I have hit a bit of a wall. I have a fairly simple app located here:
https://github.com/chuckntaylor/kwjs_flutter_demo

The app is essentially a list view of events where you can tap on one to read more details about the event.

KWJS Flutter App in action

I have two screens for this: events_screen.dart for the list view, and event_screen.dart for the details. I have been trying to write my tests in events_screen_test.dart

My testing difficulties are with the events screen (the list view). After running await tester.pumpWidget(MaterialApp(home: EventsScreen()) I can use find.text('Events') for example to find the title in the AppBar, but I cannot find any of the elements that make up the list.

To clarify further. I am using get_it as a serviceLocator to get the viewModel for the EventsScreen when it loads. The viewModel is the ChangeNotifierProvider, and EventsScreen contains a Consumer to update the list. in EventsScreen initState(), it calls loadEvents() on the viewModel. After loadEvents() is done, the viewModel calls notifyListeners(), so that EventsScreen can update.

How do I ensure that all these steps occur so that I can properly test if the EventsScreen is rendering properly?

I could be approaching this all wrong, so any advice is appreciated.

I have solved my problem, but perhaps someone can shed some light on this. In the end I executed:

await tester.pumpWidget(MaterialApp(home: EventsScreen(),));
// followed immediately by this second pump without arguments
await tester.pump();

At this point, the Widget tree was complete and I could create Finders as I hoped and could run all my expect statements without issue.

I am not sure exactly why this works though.

Hey @chuck_taylor, sorry for the late response. If I understand your issue correctly, you were facing issues finding elements to interact within your test suite.

What you did is the right approach, pumpWidget calls runApp, and also trigger a frame to paint the app. This is sufficient if your UI and data are all provided immediately from the app, or I could call them static data. (i.e., labels and texts)

When you have a structure (i.e. list, collections) with repeated data models, pump becomes essential to trigger a rebuild since the data-loading process will happen post-runApp.

@lawrey Thank you for taking the time to reply. That certainly clarifies why I didn’t have the data from the viewModel until I ran the pump method a second time. Looking forward to your next article!

1 Like

This tutorial is more than six months old so questions are no longer supported at the moment for it. Thank you!