[Flutter] Test Matcher

2024-10-09 hit count image

Let's see the Matchers available in Flutter test code and how to use them.

Outline

When writing test code in Flutter, you can use various of Matcher. In this blog post, I will introduce the Matcher available in Flutter’s test code and how to use it.

Matcher

In test code, Matcher is a verification tool used to verify specific values or conditions. When comparing whether the actual value matches the expected value or state, using Matcher allows you to write more expressive and readable test code.

In Flutter’s test code, Matcher is mainly used when checking if a value meets the expected condition through the expect statement. Matcher supports not only simple value comparisons, but also more complex conditions and various types of verification.

For example:

  • equals: You can compare if a value is the same as a specific value.
  • isNull: You can check if a value is null.
  • contains: You can check if a collection or string contains a specific item.
  • throwsException: You can check if a function throws an exception.
void main() {
  test('Example', () {
    var numbers = [1, 2, 3];
    expect(numbers, contains(2));
    expect(numbers, isNotEmpty);
    expect(() => throw Exception('Error'), throwsException);
  });
}

Using Matcher makes the test code more intuitive and readable, improving readability, and allowing you to handle complex conditions or states concisely. Also, you can combine multiple conditions or negate them, making it possible to write more flexible tests.

Frequently Used Matchers

If you check Flutter’s official documentation, you can see the available Matchers.

Here, I will introduce frequently used Matchers.

  • equals(expected): Check if the given value is the same as expected.
expect(actual, equals(expected));
  • isNot(matcher): Check if the value does not match the given matcher.
expect(actual, isNot(equals(5)));
  • isTrue / isFalse: Check if the value is true or false.
expect(value, isTrue);
  • contains(element): Check if a specific element is included in the collection or string.
expect([1, 2, 3], contains(2));
  • isNull / isNotNull: Check if the value is null or not null.
expect(value, isNull);
expect(value, isNotNull);
  • greaterThan(value) / lessThan(value): Check if the value is greater than or less than the given value.
expect(number, greaterThan(10));
  • startsWith(prefix) / endsWith(suffix): Check if the string starts or ends with a specific string.
expect('Flutter', startsWith('Fl'));
  • throwsException / throwsA(matcher): Check if a specific exception occurs.
expect(() => someFunction(), throwsException);
  • allOf(matcher1, matcher2, …): Check if all the given matchers match.
expect(value, allOf(startsWith('Fl'), contains('utt')));
  • anyOf(matcher1, matcher2, …): Check if any of the given matchers match.
expect(value, anyOf(equals(1), equals(2)));
  • isA(): Check if the value is a specific type.
expect(value, isA<int>());

Compoleted

Done! We’ve seen the Matchers available in Flutter’s test code. By using Matcher, you can write test code more intuitively and readably, and verify various conditions and states.

In my case, I’ve often used the following code in Flutter’s widget tests.

expect(titleContainer.child.runtimeType, Container);

If using the isA<T>() Matcher, you can express it more clearly as follows.

expect(titleContainer.child, isA<Container>());

I hope you can use Matcher to write test code more efficiently and readably.

Was my blog helpful? Please leave a comment at the bottom. it will be a great help to me!

App promotion

You can use the applications that are created by this blog writer Deku.
Deku created the applications with Flutter.

If you have interested, please try to download them for free.

Posts