How to do integration testing in Flutter? - flutter

I want to do integration testing in Flutter. The tutorial I follow gives the following procedure:
Add the flutter_driver package to pubspec:
dev_dependencies:
flutter_driver:
sdk: flutter
Enable the Flutter driver extension and add a call to
the enableFlutterDriverExtension() function in main.dart.
Run the integration test by using the flutter drive command:
flutter drive --target=my_app/test_driver/my_test.dart
My problem is with understanding step 2. It's not clear to me where in Android Studio do you enable the driver extension and where exactly in main.dart do you call the function enableFlutterDriveExtension().
I also have problems with the third step. After running the said command, it says in my terminal that
Error: The Flutter directory is not a clone of the GitHub project.
The flutter tool requires Git in order to operate properly;
to set up Flutter, run the following command:
git clone -b stable https://github.com/flutter/flutter.git

You have to add this code inside the test_driver/app.dart file.
import 'package:flutter_driver/driver_extension.dart';
import 'package:[YOUR_APP]/main.dart' as app;
void main() {
// This line enables the extension
enableFlutterDriverExtension();
// Call the `main()` function of your app or call `runApp` with any widget you
// are interested in testing.
app.main();
}
You can find more info on the official Flutter documentation site (Steps 3 & 4):
https://flutter.dev/docs/cookbook/testing/integration/introduction
Good luck ;)

In order to run integration test in flutter, you need to create "test_driver" directory inside app root dir. Than you need to create two files inside "test_driver" folder.
Lets call first file "app.dart" and there you need to instrument your app(answer above).
Than you need to create your test file, which needs to be called "app_test.dart" and here you write your actual test code.
When you want to run that test, just run "flutter drive --target=test_driver/app.dart".
About step 3 in your question, check if you set flutter home properly and after adding flutter_driver dependency, run "packages get".

Related

Unable to run dart command for specific device dart -d

Flutter and dart is installed successfully. I am able to run a dart file successfully using command line. e.g
//main.dart
void main() {
print('Hello, World!');
}
when i run dart main.dart i can see the output in command line but i am not able to run dart -d command i.e to run a dart test or dart file with specific device or device id.
First of all we need to separate:
The code you showed as example is native Dart without Flutter. So yes your output get only printed in the CommandLine.
If you want a Flutter Project and use the -d argument you first need to set up a Flutter Project using flutter create project_name then you can experiment around and show what you want to the world.
Useful resources which help you to get started:
Write your first Flutter app [flutter.dev Tutorial]
More or less a Flutter Roadmap

How to use --dart-define in a Flutter app embdedded in Android

I'm Embedding a Flutter app as a module within an existing Android app (https://flutter.dev/docs/development/add-to-app/android/project-setup) and would like to know if/how I can use "--dart-define" to define compile-time constants. Tried using ./gradlew -Ddart-define=myVal=Value without any luck.
When building a typical Flutter app I would use the flutter command. In my case I'm continuing to use gradlew to build my app and it's unclear how to pass in --dart-define constants.
In android studio you may edit running configuration (press drop-down near "run" button -> "Edit configurations...") and define variables there ("Additional run args:" row):
--dart-define="http_serv=http://10.0.2.2:42627/" --dart-define="websocket=ws://10.0.2.2:42627/websocket"
Getting variable in code:
final checkArgs = String.fromEnvironment('http_serv', defaultValue: '');
If your project depends on the Android Archive (AAR).
You can pass the dart-defines in the command line.
flutter build aar --dart-define=myVal=Value
If your project depends on the module’s source code.
You can set dart-defines in gradle.properties in your android host project directory (or in yourHostProject/yourFlutter/.android/Flutter/ directory).
--dart-define=myVal=Value
Explanation
.android/Flutter/build.gradle script executes "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle" script. When the flutter.gradle is executed, your dart files are compiled into naitive code.
flutter.gradle loads dart-defines from the .android/Flutter project instance (flutter.gradle#731-L734) and then, use it ( flutter.gradle#L1091-L1093).
To set the project instance value, you add --dart-define=myVal=Value into gradle.properties. (If you have multiple gradle.properties files, consider configuration order(Gradle properties in Gradle Docs).

Webdev serve : Can't load Kernel binary: Invalid kernel binary format version. No active package webdev

I want to display my mobile application on the web using Flutter. So, I have done the steps to make it.
But,when use webdev serve in command I get an error.
Can't load Kernel binary: Invalid kernel binary format version.
No active package webdev
I attach the photo below:
How I solve this problem? Thank you
Perhaps you have followed rather outdated instructions. webdev is not used anymore.
At the moment, this instruction is relevant.
flutter channel beta
flutter upgrade
flutter config --enable-web
flutter create myapp
cd myapp
flutter run -d chrome
To add web support to an existing project, run the following command in a terminal from the root project directory:
flutter create .

How to test the working of flutter package?

What would be the best practice to test the working of flutter package?
Recently I developed a Flutter package and I am testing its functionality by creating another flutter application and importing it to there, in this way I have to deal with two projects. Is there any method to do all it in the same project like android native development?
example/ directory contains only example.dart which is not runable? Any suggestion ?
Answer based off of Shahzad's flutter_tex package, and thanks to Remi's suggestion.
As mentioned, one way to test a package in Flutter is to create a Flutter app within the "Example" directory. In this app, list the following in the pubspec.yaml file:
dependencies:
flutter:
sdk: flutter
your_package_name:
path: ../
As shown, you specify your package's path, which is the level above the "Example" folder the test app resides in.

Flutter run says '...dependencies could not be established' for the meterial.dart library

I am new to flutter.io and trying to create my first flutter app according to the following guide
https://codelabs.developers.google.com/codelabs/first-flutter-app-pt1/#2
I used terminal command flutter run to start my application and then found an error as follows.
Your application could not be compiled, because its dependencies could not be established.
The following Dart file:
./startup_namer/lib/main.dart
...refers, in an import, to the following library:
/opt/flutter/packages/flutter/lib/meterial.dart
Unfortunately, that library does not appear to exist on your file system.
Couldn't find more details on how to resolve this issue.I am gussing that i should install this library package within my setup.What is the easiest method to install flutter library package from terminal?
Note: i created the app using command flutter create startup_namer
Judging by the error message you have mis-spelled the package in the import statements meterial.dart
It should be:
import 'package:flutter/material.dart';
Note: You may run into same error if you were to change the variable "name: myapp" in your pubspec.yaml file with "import 'package:myapp/settings.dart';" as the custom package in the main.dart file as was intended to add "comanyName" text to the app launcher icons. Fix: changed back to "name: myapp" removed the error