Flutter Automation Testing

You are very good in developing flutter complex mobile application for iOS & Android, but how you check you application performance, application UI testing on different devices, functional testing. So improve your application performance, Flutter provide us some package to test your application on different scenario.

Automation testing really helps here by automating some of the work that QA would do manually. We can write an automation test for those features that QA has already tested so the team can focus on testing new features while the old features will be tested automatically. This saves a lot of time and brings a higher level of confidence in shipping the app to production.

In this article we’ll explain how to do automation testing on flutter & how many types of testing you can perform.

Here three types testing we will explain:

  1. Unit Testing
  2. Widget Testing
  3. Integration Testing

Now time to write tests for our app

As discussed above, we’ll automate three types of tests for our Flutter app: unit tests, widget tests, and integration tests. An app can have several combinations of these three tests, but it’s up to you to design and implement the tests in a way that provides the most confidence for your use case.

This testes the single method of your class to ensure that the method provide the expected output based on the parameter pass.It helps you to write more testable and maintainable code.

Our goal is to write unit test case for our FieldValidator class: To be more specific we will check our login field validation on different scenario.

First add the required dependency in pubspec.yaml file.

Here is our FieldValidator class

Next, we’ll create a file to write test cases. Inside the test folder (at the root of the project), create a new file login_test.dart. It should look something like this:

Here we create a directory with the ‘test’ and inside this folder we created a dart file login_test.dart‘’

Now add the following code inside the login_test.dart file.

Here, we verify our login screen validation login by passing different inputs.The test() method will take 2 inputs. First is the description of test and second argument is a method which we call our validation method. The expect() method is a way to validate our output with expectation.The expect() first parameter is result and second is expected result.

Now we’ll run the unit test. Simply right click on login_test.dart, then tap on option ‘run tests in login_test..’

You can run this file via terminal using following command:

As the name indicate widget test use test single widget, the widget test make sure that particular widget looking & work as expected. You should write widget test for all your common widget.

We will write widget test for out login screen and ensure the widget work as expected.

First add the required dependency in pubspec.yaml file.

Similar to the login_test.dart file we created in the previous section, we’ll now create one more file loginScreen_test.dart inside the test folder. Let’s add the below code to it.

  • LoginScreen() – provides the UI for the login screen that we would normally do in the main.dart file
  • testWidgets() – creates the WidgetTester that provides ways to interact with the widget being tested.
  • await tester.pumpWidget() – renders the provided widget
  • find.text() – finds the widget with the given text. Sometimes we may have the same text in the UI, so find.byKey(Key('string')) becomes really helpful
  • expect() – takes the found widget and compares it with the expected Matcher, which can be findsOneWidget, findsNothing, etc.
  • group() perform multiple testwidgets and test inside a group, i.e. if you want to perform bunch of tests then you should write inside group.

As same above you can run this test by right click on loginscreen_test.dart, then tap on option ‘run tests in loginscreen_test..’

Integration tests help to achieve end-to-end testing for the app. They enable us to understand whether users are able to complete the full flow of the app. It’s essentially like testing a real application.

Unlike unit tests and widget tests, integration tests run on a real device or iOS simulator, android emulator , so we get a chance to see how tests are being performed.

Our goal is to test our whole application registration, login and then chat screen.

First add the required dependency in pubspec.yaml file (if you using flutter updated version).In latest version of flutter integration_test part of flutter SDK.

If you are older version on flutter then

Next, We are create a new directory at project root level with the name ‘test_driver’ (you can set any as you want). Inside this folder we create a dart file with the name ‘integration_test.dart’.(you can change name as you want, but name should be end with _test.dart).

Add following line inside the integration_test.dart file.

Next, we create another directory with name ‘integration_test’ and create a dart file authentication flow_test.dart inside it and add the following line of code.

  • await tester.tap() – clicks on the specified widget
  • await tester.enterText() — enter text in textfield.
  • await tester.pumpAndSettle() – when users click on a UI element, there might be an animation. This method ensures that the animation has settled down within a specified duration (e.g., if we think the required widget is not yet available), after which period we’re good to go for new instructions

Finally, we’ll run the test on a real device or simulator. Run following command in the terminal:

You can also run this via create a .sh file ‘execution_integration.sh’(name as you want) & write the above command inside this file.After that right click on execution_integration.sh file and select run option.




Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Vaibhav Jain

Vaibhav Jain

I am a mobile application developer, having experience around 6 years on iOS, react-native, flutter.