Update: ZoneSpecification allows overriding the print
function. By running code-under-test inside a custom zone you can capture calls to print
function. For example, the following test redirects all print messages into an in-memory list log
:
import 'dart:async';
import 'package:test/test.dart';
var log = [];
main() {
test('override print', overridePrint(() {
print('hello world');
expect(log, ['hello world']);
}));
}
void Function() overridePrint(void testFn()) => () {
var spec = new ZoneSpecification(
print: (_, __, ___, String msg) {
// Add to log instead of printing to stdout
log.add(msg);
}
);
return Zone.current.fork(specification: spec).run<void>(testFn);
};