[Dart] Check output of print in test code

[Dart] Check output of print in test code

2023-06-09 hit count image

Let's see how to check the output of print in the test code of Flutter or Dart by usint the run_with_print package.

Outline

When you develop the app with Flutter or Dart, sometimes you want to check the output of the print function. In this blog post, I will show you how to use the run_with_print package to check the output of the print function in the test code of Dart or Flutter.

Instlal run_with_print

To use the run_with_print package, execute the following command to install the run_with_print package.

dart pub add --dev run_with_print

If your project is Flutter, execute the following command to install the run_with_print package.

flutter pub add --dev run_with_print

How to use run_with_print package

In the test code, you can test the output of the print function as follows using the run_with_print package.

// ignore_for_file: avoid_print

import 'package:run_with_print/run_with_print.dart';
import 'package:test/test.dart';

void main() {
  test('Check print contents', () {
    runWithPrint((logs) {
      print('test log');
      expect(logs[0], 'test log');

      print('test message');
      expect(logs[1], 'test message');
    });
  });
}

Simply you can wrap the function or widget that has the print function with the runWithPrint function of the run_with_print package, and you can check the output of the print function through the variable passed to the runWithPrint function.

Completed

Done! We’ve seen how to use the run_with_print package to check the output of the print function in the test code of Dart or Flutter. The run_with_print package is a package I developed. If you have any feedback or suggestions, please let me know at the GitHub repository.

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