okuzawatsの日記

Android / Kotlin Enthusiast 🤖

Say Hello to Flutter Unit Testing

Let’s create a plain unit testing code of Flutter Dart code.

In this post, create a unit testing on this code. This is a simple matcher class that has one function which returns boolean value. The function checks whether the input value matches a regular expression.

/// an awesome matcher for user input
class AwesomeMatcher {
  /// matches weight is input or not.
  /// if [rawValue] in format like 100kg is input, it matches.
  bool matchWeight({String rawValue}) =>
      RegExp(r'.*[0-9]+kg').hasMatch(rawValue);
}

I will write unit testings for this class and function. At this time, Android Studio and Flutter plugin does not offer any feature to generate unit testing template code (Android Studio 4.0.1, Flutter plugin 50.0.1). So I create a dart file manually. If there is any feature to create the file, please let me know…

First of all, let’s add a dependency on test package in pubspec.yaml.

dev_dependencies:
  test:

Secondly, as the AwesomeMather is at project/lib/matcher/awesome_matcher.dart, create a corresponding file under project/test. In this case, it’s at project/test/matcher/awesome_matcher_test.dart.

Blank awesome_matcher_test.dart is below. setUp() namely do setup on each test case before it runs. tearDown() do clearup on each test case after it runs.

import 'package:awesomeapp/matcher/awesome_matcher.dart';
import 'package:test/test.dart';

void main() {
  final AwesomeMatcher awesomeMatcher = AwesomeMatcher();

  setUp(() {});

  tearDown(() {});
}

Let’s define each test case. The test case is as below. The test function takes two arguments. First is a string that describes the test case. Second is a lambda, which executes the unit testing. The expect function checks whether the test results are correct or not.

  test('matchWeight return true when 70kg', () {
    expect(awesomeMatcher.matchWeight(rawValue: '70kg'), equals(true));
  });

After defining all test cases, let’s run the unit tests. Flutter offers test command, which runs a test code. In this case, the test code is at project/test/matcher/awesome_matcher_test.dart, so the command is below.

% flutter test test/matcher/awesome_matcher_test.dart

If all of the test cases are successfully executed, the message is shown on the terminal.

00:02 +4: All tests passed!

The overall test file is below.

import 'package:awesomeapp/matcher/awesome_matcher.dart';
import 'package:test/test.dart';

void main() {
  final AwesomeMatcher awesomeMatcher = AwesomeMatcher();

  setUp(() {});

  tearDown(() {});

  test('matchWeight return true when 70kg', () {
    expect(awesomeMatcher.matchWeight(rawValue: '70kg'), equals(true));
  });

  test('matchWeight return true when foo bar 70kg', () {
    expect(awesomeMatcher.matchWeight(rawValue: 'foo bar 70kg'), equals(true));
  });

  test('matchWeight return true when foo bar 70kg baz', () {
    expect(
        awesomeMatcher.matchWeight(rawValue: 'foo bar 70kg baz'), equals(true));
  });

  test('matchWeight return false when 70', () {
    expect(awesomeMatcher.matchWeight(rawValue: '70'), equals(false));
  });
}

#Flutter #Dart

About me 😎

profile

茨城県つくば市在住のソフトウェアエンジニアです。専門領域はAndroidアプリ開発で、特にアーキテクチャに興味があります。某社でAndroidアプリ開発のテックリードをしています。

Jetpack ComposeによるAndroid MVVMアーキテクチャ入門の著者です。

👉 もっと詳しく

Writing 📝

Android MVVMアーキテクチャ入門

Androidアプリ開発の初学者に向けた、MVVM(Model-View-ViewModel)アーキテクチャの入門書を書きました。初学者の方を確実にネクストレベルに引き上げる技術書です。

👉 もっと詳しく

See Also 👀