Code Monkey home page Code Monkey logo

se_flutter_testing's Introduction

e-Portfolio Flutter Testing

The actual app is made by DennisRein. This repository just displays how to test a flutter app

Disclaimer

This is also supposed to be the handout. I didn't convert this to Pdf because converters are not so good with code. So enjoy markdown. The full tutorial is Flutter_Testing.pdf.

Installation

  1. Install flutter -> https://flutter.dev/docs/get-started/install
  2. Install Android Studio and the android sdk -> https://developer.android.com/studio/install
  3. Get a device to run the tests
  4. Add the following plugins to your pubspec.yml and run pub get
dev_dependencies:
  flutter_test:
    sdk: flutter
  flutter_driver:
    sdk: flutter
  test: any

Unit tests fundementals

  • All tests have to be in the test folder
  • All tests have to end with _test.dart
  • All test files must have a main function
  • Compare values with the expect function
  • Set up and clean up with setUp and teardown
  • Consider the following code for reference
import 'package:flutter_test/flutter_test.dart';
import 'package:sepokedex/model.dart';

void main() async {
  Pokemon pokemon;

  setUp(() {
    print('I run before each and every test');
  });

  setUpAll(() {
    print('I run once before everything else');
  });

  tearDown(() {
    print('I run after each test');
  });

  group('group', () {

    tearDownAll(() {
      print('I run once after this group');
    });

    test('has the correct name', () async {
      int sum = 1 + 2;

      expect(sum, 3);
    });

  });
}
  • Execute tests from your flutter root with the command
flutter test
  • If you want code coverage. Visualized on codacy
flutter test --coverage

Integration tests fundementals

  • All tests have to be in the test_driver folder
  • Each test consists out of two files
    • A file called like you want. E.g app.dart
    • A test file called app_test.dart

app.dart contains everything to execute the test. It sets up the test and runs the app. Minimal file is as follows.

import 'package:flutter_driver/driver_extension.dart';
import 'package:sepokedex/main.dart' as app;

void main() {
  // Enable the flutter driver extension to run and capture tests
  enableFlutterDriverExtension();

  // Configure things here before the app is launched.

  // Execute the main app
  app.main();
}

app_test.dart contains the tests you want to execute on your app. Structure is similar to an unit test.

Finding a widget

  • In order to find a widget you have to give it a key
Align(
    alignment: Alignment.centerLeft,
    child: Text(
        pokemon.name,
        key: Key('pokemonName'),
        style:
            Theme.of(context).textTheme.headline1.copyWith(fontSize: 72),
    ),
),
  • Then use the method byValueKey of the built-in object finder
final pokemonName = find.byValueKey('pokemonName');

Interacting with a widget

  • Tap it
await driver.tap(firstPokemon);
  • Write to anything that is a text field and currently focused
driver.enterText('Text to enter')

Execute tests from your flutter root with the command

flutter drive --target=test_driver/app.dart

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.