From 757eca26f1e54713201973e6a793a456fffc6272 Mon Sep 17 00:00:00 2001 From: Donne Martin Date: Sat, 1 Aug 2015 09:24:19 -0400 Subject: [PATCH] Added unit testing utility to redirect print output to help validate some functions. --- graphs_trees/utils/captured_output.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 graphs_trees/utils/captured_output.py diff --git a/graphs_trees/utils/captured_output.py b/graphs_trees/utils/captured_output.py new file mode 100644 index 0000000..99aaf0e --- /dev/null +++ b/graphs_trees/utils/captured_output.py @@ -0,0 +1,14 @@ +import sys +from contextlib import contextmanager +from StringIO import StringIO + + +@contextmanager +def captured_output(): + new_out, new_err = StringIO(), StringIO() + old_out, old_err = sys.stdout, sys.stderr + try: + sys.stdout, sys.stderr = new_out, new_err + yield sys.stdout, sys.stderr + finally: + sys.stdout, sys.stderr = old_out, old_err \ No newline at end of file