/
13/06/2022 1 APP010-2 -  MOBILE APPLICATIONS 13/06/2022 1 APP010-2 -  MOBILE APPLICATIONS

13/06/2022 1 APP010-2 -  MOBILE APPLICATIONS - PowerPoint Presentation

wilson
wilson . @wilson
Follow
0 views
Uploaded On 2024-03-13

13/06/2022 1 APP010-2 -  MOBILE APPLICATIONS - PPT Presentation

Lecture 12 Testing amp Plugins Testing Integration Integration tests are written using the  integrationtest  package https githubcomflutterfluttertreemainpackagesintegrationtest ID: 1048155

flutter test counter integration test flutter integration counter run widget dart package create app timeline tester tests title introduction

Share:

Link:

Embed:

Download Presentation from below link

Download Presentation The PPT/PDF document "13/06/2022 1 APP010-2 -  MOBILE APPLICA..." is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.


Presentation Transcript

1. 13/06/20221APP010-2 - MOBILE APPLICATIONS Lecture 12: Testing & Plugins

2. TestingIntegration- Integration tests are written using the integration_test package (https://github.com/flutter/flutter/tree/main/packages/integration_test) , provided by the SDK. Unit The test package (https://pub.dev/packages/test) provides the core framework for writing unit testsWidgetTo test widget classes, you need a few additional tools provided by the flutter_test package (https://api.flutter.dev/flutter/flutter_test/flutter_test-library.html) , which ships with the Flutter SDK.13/06/20222

3. An introduction to integration testingCreate an app to test.Add the integration_test dependency.Create the test files.Write the integration test.Run the integration test.

4. Run the integration test.a. MobileTo test on a real iOS / Android device, first connect the device and run the following command from the root of the project:flutter test integration_test/app_test.dartOr, you can specify the directory to run all integration tests:flutter test integration_test13/06/20224

5. Run the integration test (Cont..)5b. WebTo get started testing in a web browser, Download ChromeDriver. import'package:integration_test/integration_test_driver.dart';Future<void> main() => integrationDriver();Launch WebDriver, for example:chromedriver --port=4444From the root of the project, run the following command:flutter drive \ --driver=test_driver/integration_test.dart \ --target=integration_test/app_test.dart \ -d web-server13/06/20225

6. Performance profiling (Cont..):There are two options: first, manually test the app on different devices. Alternatively, run an integration test that performs a specific task and records a performance timeline. Then, examine the results to determine whether a specific section of the app needs to be improved.In this recipe, learn how to write a test that records a performance timeline while performing a specific task and saves a summary of the results to a local file.This recipe uses the following steps:Write a test that scrolls through a list of items.Record the performance of the app.Save the results to disk.Run the test.Review the results.13/06/20226

7. 1- Write a test that scrolls through a list of items.13/06/20227To focus on performance profiling, this recipe builds on the Scrolling recipe in widget tests.2- Record the performance of the app// Example shows the reportKey is changed to scrolling_timeline.await binding.traceAction( () async { // Scroll until the item to be found appears. await tester.scrollUntilVisible( itemFinder, 500.0, scrollable: listFinder, );}, reportKey: 'scrolling_timeline',);

8. 3 - Save the results to disk13/06/20228 4 - Run the testflutter drive \ --driver=test_driver/perf_driver.dart \ --target=integration_test/scrolling_test.dart \ --profile

9. 13/06/202295 - Review the resultsscrolling_summary.timeline_summary.json contains the summary. Open the file with any text editor to review the information contained within. With a more advanced setup, you could save a summary every time the test runs and create a graph of the results.scrolling_timeline.timeline.json contains the complete timeline data. Open the file using the Chrome browser’s tracing tools found at chrome://tracing. The tracing tools provide a convenient interface for inspecting the timeline data to discover the source of a performance issue.

10. An introduction to unit testing13/06/2022101- Add the test dependencydev_dependencies: test: <latest_version>counter_app/ lib/ counter.dart test/ counter_test.dart2- Create a test file

11. 13/06/202211An introduction to unit testing (Cont…)3- Create a class to testclass Counter { int value = 0; void increment() => value++; void decrement() => value--;}4- Write a test for our class// Import the test package and Counter classimport 'package:test/test.dart';import 'package:counter_app/counter.dart';void main() { test('Counter value should be incremented', () { final counter = Counter(); counter.increment(); expect(counter.value, 1); }); }

12. 13/06/2022125. Combine multiple tests in a groupAn introduction to unit testing (Cont…)Combine them using the group function provided by the test package.6. Run the testRun tests using IntelliJ or VSCodeIntelliJOpen the counter_test.dart fileSelect the Run menuClick the Run 'tests in counter_test.dart' optionAlternatively, use the appropriate keyboard shortcut for your platform.VSCodeOpen the counter_test.dart fileSelect the Run menuClick the Start Debugging optionAlternatively, use the appropriate keyboard shortcut for your platform.

13. 13/06/202213Mock dependencies using MockitoAdd the package dependencies.Create a function to test.Create a test file with a mock http.Client.Write a test for each condition.Run the tests.For more information, see the Mockito package documentation at https://pub.dev/packages/mockito

14. An introduction to widget testingTo test widget classes, you need a few additional tools provided by the flutter_test packagehttps://api.flutter.dev/flutter/flutter_test/flutter_test-library.htmlwhich uses the following steps:Add the flutter_test dependency.Create a widget to test.Create a testWidgets test.Build the widget using the WidgetTester.Search for the widget using a Finder.Verify the widget using a Matcher.13/06/202214

15. An introduction to widget testing (Cont..)1- Add the flutter_test dependency dev_dependencies: flutter_test: sdk: flutter2- Create a widget to test3- Create a testWidgets testvoid main() {testWidgets('MyWidget has a title and message', (tester) async {});}13/06/202215

16. 13/06/2022164- Build the widget using the WidgetTestervoid main() { testWidgets('MyWidget has a title and message', (tester) async {await tester.pumpWidget(const MyWidget(title: 'T', message: 'M')); }); }5- Search for our widget using a Findervoid main() { testWidgets('MyWidget has a title and message', (tester) async { await tester.pumpWidget(const MyWidget(title: 'T', message: 'M'));final titleFinder = find.text('T'); final messageFinder = find.text('M'); });}

17. 6- Verify the widget using a Matchervoid main() { testWidgets('MyWidget has a title and message', (tester) async { await tester.pumpWidget(const MyWidget(title: 'T', message: 'M')); final titleFinder = find.text('T'); final messageFinder = find.text('M');expect(titleFinder, findsOneWidget); expect(messageFinder, findsOneWidget); });}13/06/202217An introduction to widget testing (Cont..)

18. Find widgetsTo locate widgets in a test environment, use the Finder classeshttps://api.flutter.dev/flutter/flutter_test/Finder-class.htmlusing the tools provided by the flutter_test package.https://api.flutter.dev/flutter/flutter_test/flutter_test-library.html13/06/202218