mirror of
https://github.com/donnemartin/interactive-coding-challenges
synced 2026-01-07 01:48:02 +00:00
Added unit testing utility to redirect print output to help validate some functions.
This commit is contained in:
14
graphs_trees/utils/captured_output.py
Normal file
14
graphs_trees/utils/captured_output.py
Normal file
@@ -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
|
||||
Reference in New Issue
Block a user