Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Flutter testing

Flutter testing

Agenda:
- Unit & Widget Test
- Advantages of unit test.
- Why Mockito?
- Type of Matcher, Finders & important functions.
- Example of unit, widget test and mockito

Avatar for Kashifa Laliwala

Kashifa Laliwala

June 25, 2021

Other Decks in Programming

Transcript

  1. Agenda • Unit & Widget Test • Advantages of unit

    test. • Why Mockito? • Type of Matcher, Finders & important functions. • Example of unit, widget test and mockito
  2. What is Unit Test? • Unit test are there to

    test the logic of application. • They are used to verify the behaviour of a single function, methods or class. • Test cases comprise of the call of test(name, fn). The fi rst parameter is the name (or a short description) of the test. The second parameter is a function that is executed and contains the actual logic being tested.
  3. Advantages of Unit Test • Reduce defects/errors in SDLC. •

    Cost of testing is reduced as majority of errors are captured in early phase • Simpli fi ed debugging process.
  4. Example counter_app / lib / counter.dar t test / counter_test.dar

    t • Test fi les should always end with _test.dart, this is the convention used by the test runner when searching for tests. void main() {
 test('Counter value should be incremented', () { final counter = Counter(); counter.increment(); expect(counter.value, 1); }) ; } class Counter { int value = 0; void increment() => value++; void decrement() => value-- ; } dev_dependencies : flutter_test: Step 1: Step 2: Step 3: Step 4:
  5. In certain cases, unit tests may depend on classes that

    fetch data from live web services or databases. This is inconvenient for a few reasons: • Calling live services or databases slows down test execution. • A passing test may start failing if a web service or database returns unexpected results. This is known as a “ fl aky test.” • It is di ffi cult to test all possible success & failure scenarios using a live web service or database.
  6. Mockito • You can “Mock” (replica) this dependencies using Mockito

    package. • Mocks allow copying a live web service or database which allows us to test speci fi c results under di ff erent conditions.
  7. • Group: We can group multiple test with the help

    of this function. It can be written as group(short description, () {}) • Setup: This callback is called before running each test individually/within a given group. setUp(() {}) • TearDown: This callback is called after running each test under a given group/individually. It should contain any cleanup code that has to run post each test. It is written as tearDown(() {})
  8. dependencies: 
 mockito: ^5.0.10 dev_dependencies: build_runner: ^2.0.4 Step 1: Step

    2: Create a class which 
 you want to mock. Step 3: Annotate Test Class with
 @GeneratedMocks[Album] An annotation to direct Mockito to generate mock classes. Step 4: Write an import statement for 
 mock
 eg. import will be
 test_class_name.mocks.dart
 Step 5: Run dart run build_runner build
 to generate mock class.
 Mock class will be named as 
 MockAlbum
 Step 6: You can start writing test results by calling generated mock object
 var album = MockAlbum();

  9. What is Widget Test? • It is a test that

    tests the single widget. • The goal with widget test is to verify that the UI looks and interacts as expected.
  10. The fl utter_test package provides following tools for testing widgets.

    • The testWidgets() creates a new WidgetTester for each test case. • The WidgetTester class allows building and interacting with widgets in a test environment. • The Finder class allow searching for widgets in the test environment. • The Matcher constant help verify whether a Finder locates a widget or multiple widgets in the test.
  11. Type of Matchers for common cases • FindsOneWidget - Veri

    fi es that widget appear on screen exactly one time. • FindsNothing - Veri fi es that no widget are found. • FindsWidgets - Veri fi es that one or more widget are found. • FindsNWidgets- Veri fi es that speci fi c number of widgets are found. • matchesGoldenFile - Veri fi es that the widgets rendering matches a particular bitmap image.
  12. Finder class In order to perform some actions on a

    nested widget, you need to fi nd it in the widget tree. To do this, there is a global object fi nd, which allows you to fi nd widgets: • in the tree by text — fi nd.text, fi nd.widgetWithText;
 -> Only di ff erence between the two is we need to pass widget in widgetWithText • based on the key — fi nd.byKey; • by icon — fi nd.bylcon, fi nd.widgetWithIcon; • by type — fi nd.byType; • by position in the tree — fi nd.descendant • Finds widgets using a widget [predicate]. — fi nd.byWidgetPredicate. expect( fi nd.byWidgetPredicate( (Widget widget) => widget is Tooltip && widget.message == 'Back', description: 'widget with tooltip "Back"', ), fi ndsOneWidget);
  13. Important Functions • pumpWidget — create a widget to test;

    
 eg. 
 await tester.pumpWidget(myApp()); • pump — start the process of changing a widget’s state and wait for its completion with the given duration (100 ms by default); 
 eg. 
 await tester.tap(find.byIcon(Icons.add)); 
 await tester.pump(); • pumpAndSettle — repeatedly call pump in a state change cycle with the given duration (100 ms by default) and wait for all animations to complete; • tap — send a tap to the widget; • longPress — send a long press; • fling — a fling/swipe; eg. scroll • drag — dragging; eg. Swipe Dismiss • enterText — enter text into a TextField.
  14. Example testWidgets('Counter increments smoke test', (WidgetTester tester) async{ 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); });