Flutter is an open source framework to create high quality, high performance mobile applications across mobile operating systems - Android and iOS. It provides a simple, powerful, efficient and easy to understand SDK to write mobile application in Google's own language, Dart. In the terminal, run the flutter devices command to verify that Flutter recognizes your connected Android device. By default, Flutter uses the version of the Android SDK where your adb tool is based. Step 13: install the release apk on a device Connect your Android device to your computer with a USB cable. Cd where is your application directory Run flutter install. Step 14: Publishing an APK to the Google Play Store Thats it. The Content stated above is for informational purpose only. Vst plugins for.
Connect to the Internet. During the run of your very first Flutter app, Android Studio downloads some additional software. Select the Start a New Flutter Project option. On your phone, an app is an app and that's all there is to it. 使用flutter搭建了一个简单的示例,在android studio运行flutter run时提示:No connected devices found; please connect a device, or see flutter.io/setup for getting started instructions.从提示可以看到,原因是flutter没有找到连接的设备或者模拟器。.
Since its initial release, Flutter has quickly gained its popularity among developers for building beautiful Android and iOS applications. Like apps built with any other development toolkit, automated testing of Flutter apps is the only way to ensure app quality in the shortest time possible.
In this article, I'd like to talk about how to create the unit, widget and integration tests for automating the testing of Flutter apps and execute them against real Android devices in Bitbar Cloud.
Creating a Sample Bitbar App with Flutter SDK
To better understand how to automate Flutter app testing, I started creating a Bitbar sample app using Flutter SDK (see UI below).
The MainPage looks like this:
Text element
- 3 button elements (
RaisedButton
) TextField
element- Image asset element
The SubPage looks like this:
- 2
Text elements
- button element (
RaisedButton
) TextField
element- Image asset element
In my opinion, the easiest way to create a new Flutter app is to use the flutter create
command, for example: flutter create my_app
. This will create a sample app for Android and iOS.
I created the sample app by modifying this sample app. The app source is in a file called main.dart
, and it is in the lib
directory.
Note: I gave all the important UI elements Key values, for example:
Creating Unit and Widget Tests
A ‘unit test‘ is to test a single method or class and a ‘widget test‘ is to test a single widget. Here, a ‘widget‘ means UI elements like layout, button, text box, etc.
Unit tests require a test
package (https://pub.dev/packages/test), and the flutter_test
package provides additional tools for widget testing.
1. Add the test or flutter_test dependency
You can use the following approach to include test
or flutter_test
(or both) dependency on the app's pubspec.yaml
file:
2. Create a unit test file
Create a test
directory and test file inside that directory. It is also a good idea to make separate directories for unit and widget tests.
In this example, we can create a file ending with _test.dart
for example main_test.dart
.
Import packages:
Sample test:
3. Create a widget test file
Again create a file ending with _test.dart
.
Import packages:
Sample test:
4. Use command to run the tests
Unit tests can be run with the command:
And widget tests can be executed with the command:
5. JUnit Report
One thing to note: If the unit and widget tests are executed in Bitbar Cloud within a CI tool e.g. Jenkins, the test results will not display correctly after the tests are finished.
To get over this, we can use a Flutter package to convert the test results to the JUnit XML report format. (https://pub.dev/packages/junitreport).
First, make sure the following stuff is included in the system path:
flutter/.pub-cache/bin
flutter/bin/cache/dart-sdk/bin
Flutter Connect Android Device Android
Then, install the junitreport
package by running the command below:
Both unit and widget tests can be run with this command:
After the tests are finished, the unit and widget test results can be found in a file called TEST-all.xml
. Bluestacks video player not working.
Creating Integration Tests
An integration test tests the complete app and is isolated from the app under test. Integration tests require a flutter_driver
package (https://api.flutter.dev/flutter/flutter_driver/flutter_driver-library.html).
Flutter driver:
- The application runs in a separate process from the test itself
- Android (Espresso)
- iOS (Earl Grey)
- Web (Selenium WebDriver)
1. Add the flutter_driver dependency
The flutter_driver
package needs to be added to dev_dependencies
section of the app's pubspec.yaml
file:
2. Create an integration test file
Create a directory called test_driver
. Add files main.dart
and main_test.dart
(or something ending with _test
) inside that directory.
Main.dart
Best Android Device
This first contains an ‘instrumented' version of the app being tested. It enables Flutter driver extensions and runs the app.
Main_test.dart
This file is created to contain the test suite.
Import Flutter Driver API:
Connect to the app in 'setUpAll' method:
Close connection in 'tearDownAll' method:
Flutter Android Plugin
3. Write a sample integration test:
4. Use command to run the integration tests
Now that we have an instrumented app and a test suite, we can run integration tests with the following command:
There doesn't seem to be a ready package for converting integration test results in a format that could be read in a CI tool, at least at the time of writing this. Test results must be parsed and converted to Junit XML format or something else.
5. Take screenshots
Screenshots can be taken in the integration tests with the ‘screenshots' package (https://pub.dev/packages/screenshots).
Add the dependency in the pubspec.yaml
file (the current version of the package at the time of writing this blog):
Create the screenshots.yaml
file inside the project root directory. It should look something like this:
Add these in the main_test.dart
file: Bluestacks new version.
Import dependency:
Create a config:
Take screenshot inside test method like this:
Testing Flutter Android Apps on Real Devices in Bitbar Cloud
In Bitbar Real Device Cloud, Flutter tests are executed under the Appium Server Side type test project. The ‘run-tests.sh' shell script is used to run the Flutter tests. Integration, Unit and Widget tests can be run in the same project in the cloud.
Note that only integration tests actually install and run the test in a device, unit and widget tests don't require devices to run. Device time is still spent when running unit or widget tests, the device is just idling while the test is running.
See a quick demo below.
Unit and Widget tests
Install JUnit report:
Run unit and widget tests:
Move test results to root directory so that they can be found by Jenkins:
Below is the content of the ‘run-tests.sh' file
Integration tests
Run tests with the command:
Parse results and convert them into a Junit .xml file called ‘TEST-all.xml' or just look at the log file (console.log) after the test run has ended. Move screenshots to the directory in the root called ‘screenshots':
How to create and start a Flutter test run in Bitbar Cloud
1. Create a zip-file containing app directory (app and tests, in my case ‘my_app' directory) and ‘run-tests.sh' file.
2. Select Android as your target OS type and select the ‘Appium Android Server Side' type as the framework.
3. Upload your test files to the ‘Appium Server Side' type project in Bitbar Cloud.
Note: Since the actual Flutter app to test is written in the ‘flutter-tests.zip' file and will be built during the test run, you could upload any dummy .apk file, e.g. ‘bitbar-sample-app.apk' (see below) to get the test run started on Bitbar Cloud.
4. Start your Flutter test and get the test results
Conclusion
Testing Flutter apps in Bitbar Cloud is available on real Android devices. Integration test results are not showing correctly without parsing and formatting them yourself. Note that examples in this article have nothing to do with the Appium test automation framework.