ABSTRACT

In this chapter, you will learn the basics of testing widgets (Widget Testing) in a Flutter application. In widget testing, widgets are tested interactively. We will use the improvised default CounterApp example from the previous chapter (Chapter 21: Unit Testing). Earlier, we used the CounterApp to demonstrate writing unit tests for Flutter applications. In this chapter, you will use the same CounterApp to learn writing tests for this app's widgets. We will test the custom Text widget `MyTextWidget` used for displaying label text and counter number.

Additionally, we will test the `ElevatedButton` widget to increment and decrement the number on the screen. In the last chapter, the `test` package was enough to write unit tests for pure Dart code. For testing Flutter widgets, you need tools shipped with the `flutter_test` package. You will learn to use WidgetTester, testWidgets(), Finder, and Matcher to test the widgets’ behavior. The WidgetTester helps build interactive test widgets. The testWidgets()methods are used to write test code for verifying a widget's behavior. The Finder class helps search for widgets in the test app. Finally, the Matcher class helps verify whether the Finder class could locate a given widget in the test environment.