Testing is a very important phase in application development. This ensures that the application is of high quality. The purpose of the test is to prove that the widget's user interface looks and interacts as planned. The tested widget has the ability to receive user actions and events, and respond to them, build a tree of child widgets. Therefore, widget tests are more complex than unit tests.
So, let's create a simple test application.

Screenshot

This is a simple application that consists of three widgets. We write text in a textfield and by pressing the "Send" button the text is displayed in a text widget.

Widget

For writing test widgets in our pubspec.yaml, flutter_test sdk has already been added when project created.

Dependencies

now let's write a widget test

Test

Use the testWidgets() function provided by the flutter_test package to define a test. The testWidgets function allows you to define a widget test and creates a WidgetTester to work with.
This test verifies that MyWidget displays a given title and message.
Next, build MyWidget inside the test environment by using the pumbWidget() method provided by WidgetTester. The pumpWidget method builds and renders the provided widget.
With a widget in the test environment, search through the widget tree for the button and text Text widgets using a Finder. This allows verification that the widgets are being displayed correctly.
In line 10 we use the find.byType() method to search for TextField.
find.byType() - looking for any widget.
Make sure all widgets appear on the screen using the matcher constants provided by flutter_test. Since we have two text widgets we need to use a findsWidget, but for the button we use findsOneWidgets.
Since the state of our widgets is changing, we need to rebuild them after clicking on the button, these are the 18th and 19th lines of our code.
In line 18 we press the button and in line 19 we rebuild our widget using the tester.pumb() method, this is an analog of the setState() method.
Now we need to run the flutter test method from the terminal and if all the tests work out correctly, we will get the answer All tests passed!
But what if it’s not enough for us and we want to use CI/CD which will automatically check all our builds and send the test results directly to our mail. And for this there is an excellent CI/CD that works with flutter directly - this is the CodeMagic.
choose your development environment

CodeMagic

Next we need to go to the settings of our project

Settings

check the box in the test section for testing and save the changes

Check the box in the test section

Now you can start your testing.
For a detailed study of the test widget, you can use the links:

 

Flutter
Development
QA

More like this

Get in touch

Get in touch

Kharkiv, Ukraine

61023

Trinklera street, 9

+38 (050) 908-31-07

info@a5.ua

Frankfurt am Main, Germany

60354

Eckenheimer Schulstraße, 20

+38 (098) 630-49-85

info@a5.ua