How to reinstall the app between every testWidgets in Flutter integration test? - flutter

In Flutter integration tests, is there any way we can kill the app & reinstall it for every testWidgets?
void main() {
IntegrationTestWidgetsFlutterBinding.ensureInitialized();
testWidgets('TC_01 Home page validation', (tester) async {
app.main();
await tester.pumpAndSettle(const Duration(seconds: 5));
await tester.tap(find.byKey(const ValueKey('startButton')));
await tester.pumpAndSettle();
});
testWidgets('TC_02 Home page text validation ', (tester) async {
app.main();
await tester.pumpAndSettle(const Duration(seconds: 5));
expect(find.text('Home Screen'), findsOneWidget);
expect(find.text('Transactions'), findsOneWidget);
});
}
I want to reinstall the app for every new test that runs.

This is not possible, because, in Flutter, the tests (integration tests included) live inside your app. If you uninstalled the app after the first test passed, you'd also uninstall the test suite (which runs inside your app) - which doesn't make sense.
A better question is to ask: what do you want to achieve? If you want to reset state, you can pump your app once again and it will reset the whole widget tree, along with all blocs/providers you might've created.

Related

How Can I Restart My Flutter App With Getx

I am using the getx package in my application and there is something I am wondering about. Is it possible to reset all states of my app using getx?
So it's kind of like a restart.
Future<void> logoutUser() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
prefs.clear(); //clearing my token data
await Get.deleteAll(force: true); //deleting all controllers
Phoenix.rebirth(Get.context!); // Restarting app
Get.reset(); // resetting getx
}
used flutter_phoenix library to restart the app.
Deleted all GetX controllers using Get.deleteAll(force: true);
Works like a charm for me.

How to test canvas with testWidgets in a flutter mobile app project

I was reading about how to build tests for flutter app widgets with the testWidgets function, like this test which comes by default when creating a new flutter project:
// <project>/test/widget_test.dart
void main() {
testWidgets('Counter increments smoke test', (WidgetTester tester) async {
await tester.pumpWidget(CounterApp());
// validate counter starts at zero
expect(find.text('0'), findsOneWidget);
await tester.tap(find.byIcon(Icons.add));
await tester.pump();
expect(find.text('0'), findsNothing);
expect(find.text('1'), findsOneWidget);
}
}
But how would one test a canvas to see if the drawings/patterns and paragraphs are being displayed correctly? Does the finder object finds a Text widget if I draw a paragraph with canvas.drawParagraph(...)? I couldn't find info about this in the docs.
Testing Canvas in general (no matter the technology) should be performed using visual testing (or visual regression testing).
flutter_test package provides matchesGoldenFile function that makes it possible. There's a nice tutorial on Flutter's Github that will guide you through.

ERROR in flutter: widget_test.dart cannot detect MyApp() pls helpme

Being a total beginner I am trying out various flutter feature and I am stuck at running the main.dart due to errors in the widget_test.dart file. Please point out if the error is due to some other reason.
main.dart
import 'package:flutter/material.dart';
import 'package:food_app/pages/HomePage.dart';
import 'package:food_app/pages/StarterPage.dart';
void main() => runApp(
MaterialApp(
debugShowCheckedModeBanner: false,
theme: ThemeData(fontFamily: 'Roboto'),
home: StarterPage(),
)
);
widget_test.dart
// This is a basic Flutter widget test.
//
// To perform an interaction with a widget in your test, use the WidgetTester
// utility that Flutter provides. For example, you can send tap and scroll
// gestures. You can also use WidgetTester to find child widgets in the widget
// tree, read text, and verify that the values of widget properties are correct.
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:food_app/main.dart';
void main() {
testWidgets('Counter increments smoke test', (WidgetTester tester) async {
// Build our app and trigger a frame.
await tester.pumpWidget(MyApp());
// Verify that our counter starts at 0.
expect(find.text('0'), findsOneWidget);
expect(find.text('1'), findsNothing);
// Tap the '+' icon and trigger a frame.
await tester.tap(find.byIcon(Icons.add));
await tester.pump();
// Verify that our counter has incremented.
expect(find.text('0'), findsNothing);
expect(find.text('1'), findsOneWidget);
});
}
This widget_test.dart file is a file for your tests (obviously). It is separate of your main code and its purpose is only for writing unit, widget, integration and probably some other tests.
I believe that your question came up because your IDE shows you a problem in that code. You don't have to worry about it. As long as you run flutter run or flutter build tests aren't touched.
You can delete the whole tests/ directory if you don't plan on maintaining them.
I deeply suggest you learn about Flutter unit and widget tests as you and your skills will benefit from it, it is also an industry standard to write tests. You can learn more about them in Testing Flutter apps. If you're only getting started with Flutter, you can leave it for later though.
However, if you plan to fix those test, firstly, you should put your MaterialApp in some custom widget, for example FoodApp (guessing from the package name) and pump it in tests. You will need to tweak those tests a little bit more than that. Please refer to the resource I linked above.

Load Data during Splash Screen in Flutter

I need to know is there any way to perform a task during splash screen is visible in flutter. I know how to add splash screen in flutter but I don't know how to perform background operations during splash screen is visible. I can't find anything on the internet, please help.
Yes, yes you can. The main() function can actually be tagged as async, so you can do whatever you need to do before running runApp(...) in the main() method body, even asynchronously. This way, the splash screen will be shown until your asynchronous result is retrieved, before calling runApp(...). For example:
Future<void> main() async {
// Do whatever you need to do here
final home = await setHomeWidgetDependingOnLoginStatus();
return runApp(MyApp(home: home));
}
The animated_splash_screen package does this worderfully. add it to your pubspec.yml
animated_splash_screen: ^1.1.0
AnimatedSplashScreen.withScreenFunction(
splash: 'images/splash.png',
screenFunction: () async{
// Your code here
return MainScreen();
},
splashTransition: SplashTransition.rotationTransition,
pageTransitionType: PageTransitionType.scale,
)
This will load and hold you splash screen until your function is complete.
Your function has to return a widget, in this case is your home screen. This also allows for dynamic routing depending on your data eg, is user logged in.

In Flutter widget testing, how to pause app to find suggested Finder?

I am following flutter widget testing as https://flutter.io/testing/#widget-testing.
It says that
simply flutter run test/widget_test.dart to see your test run in your preferred runtime environment such as a simulator or a device. During a flutter run session on a widget test, you can also interactively tap parts of the screen for the Flutter tool to print the suggested Finder.
I am using flutter run test/widget_test.dart so that i can tap and get suggested finders. But I am not able to pause the test so that i can actually tap. The test runs and automatically ends. So that there are no widgets on the screen except Text('Test Finished').
Thank you very much!
I know it's been almost 2 years since the question was asked, but anyway. tester.idle() helped me to pause an app.
testWidgets('idle', (WidgetTester tester) async {
await tester.pumpWidget(
//
);
await tester.tap(find.byIcon(Icons.bluetooth));
await tester.pump();
tester.idle();
});