Thursday, March 24, 2022

Strategy for Implementing and Using Autotests in Flutter

Full testing requires both Widget and E2E tests.

Widget tests. Well suited for launching on pull requests. They work on mock data and pass several times faster.

E2E tests. It is necessary to test the application on full user scenarios and a real server - otherwise it is difficult to assert that the system is fully operational. At the same time, it is not advisable to run them, for example, on pull requests: E2E tests are slow and unstable. Their instability is not in the way they are written, but in the infrastructure: the server can always crash, some request may fall off, or a system dialog will appear on a real device. All this will drop the test.

Autotests are most useful if you run them on stable features as often as possible.


New package

At the end of 2020, Flutter updated the flutter_test package. Prior to this, Flutter worked with Widget and E2E tests separately.

For Widget components, there was a WidgetTester class. It allowed downloading a Widget and checking it out. For E2E, the FlutterDriver class. It loaded the application and allowed you to simulate interaction with the interface. Now only WidgetTester remains: Flutter has deprecated the driver approach.

The first thing that catches your eye is that everything has become easier. E2E tests now work through the same API as Widget tests - inside the flutter_test package via WidgetTester. Therefore, it has become easier to implement support for Widget and E2E tests at the same time.

Can test automation be automated?

When we started developing with Flutter at Surf, it became interesting to see what Flutter itself can automate? After all, the applications that he creates are cross-platform, which means that autotests will be like that ... However, a stable package for working with E2E and Widget tests was included in the Flutter framework not so long ago - in early December 2020. Therefore, it is interesting to discuss this topic.

Today we will talk about autotesting within the Flutter framework in the context of mobile applications.

Autotests on Flutter

Flutter makes it possible to write autotests natively in the Dart language. The framework has:

  • Unit tests, their task is to test a specific module of the system. For example, make sure that the component's controller exposes the desired state. Since this is not an interface check, there is no need to emulate the application.
  • Widget tests. Any Flutter application consists of Widget components: Widget tests allow you to emulate a Widget and carry out the necessary testing with it. These can be entire screens and individual elements: fields, buttons, checkboxes, and so on.
  • End-to-end (hereinafter - E2E) - tests in which we fully load the application and simulate user actions in a real infrastructure with real services, APIs, and so on.

Strategy for Implementing and Using Autotests in Flutter

Full testing requires both Widget and E2E tests. Widget tests. Well suited for launching on pull requests. They work on mock data and pass s...