Is there is any way to capture print() statement during integration test

Viewed 145

Is there is any way during the integration testing to monitor or save or read whatever the print() statement is printing to the console. I m using integration_test for testing.

1 Answers

Maybe this will help you?

import 'dart:async';
import 'dart:developer';

void main(List<String> args) async {
  final printed = <String>[];
  final result = runZoned(() => foo(), zoneSpecification: ZoneSpecification(
    print: (self, parent, zone, line) {
      printed.add(line);
    },
  ));

  print('Result: $result');
  print('Printed:\n${printed.join('\n')}');
  debugger();
}

int foo() {
  print('Hello');
  print('Goodbye');
  return 41;
}

P.S.
I added a debugger call so that the result of the work was visible. This statement can (and should) be removed.

Related