Skip to content

Commit 5692e70

Browse files
committed
Allow test runner to run while python unittest captures stdout
1 parent 79005fb commit 5692e70

1 file changed

Lines changed: 14 additions & 11 deletions

File tree

vunit/test_runner.py

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
LOGGER = logging.getLogger(__name__)
2525

2626

27-
class TestRunner(object):
27+
class TestRunner(object): # pylint: disable=too-many-instance-attributes
2828
"""
2929
Administer the execution of a list of test suites
3030
"""
@@ -35,6 +35,8 @@ def __init__(self, report, output_path, verbose=False, num_threads=1):
3535
self._output_path = output_path
3636
self._verbose = verbose
3737
self._num_threads = num_threads
38+
self._stdout = sys.stdout
39+
self._stderr = sys.stderr
3840

3941
def run(self, test_suites):
4042
"""
@@ -59,8 +61,8 @@ def run(self, test_suites):
5961
write_stdout = self._verbose and self._num_threads == 1
6062

6163
try:
62-
sys.stdout = ThreadLocalOutput(self._local)
63-
sys.stderr = ThreadLocalOutput(self._local)
64+
sys.stdout = ThreadLocalOutput(self._local, self._stdout)
65+
sys.stderr = ThreadLocalOutput(self._local, self._stdout)
6466

6567
# Start P-1 worker threads
6668
for _ in range(self._num_threads - 1):
@@ -83,15 +85,15 @@ def run(self, test_suites):
8385
for thread in threads:
8486
thread.join()
8587

86-
sys.stdout = sys.__stdout__
87-
sys.stderr = sys.__stderr__
88+
sys.stdout = self._stdout
89+
sys.stderr = self._stderr
8890
LOGGER.debug("TestRunner: Leaving")
8991

9092
def _run_thread(self, write_stdout, scheduler, num_tests, is_main):
9193
"""
9294
Run worker thread
9395
"""
94-
self._local.output = sys.__stdout__
96+
self._local.output = self._stdout
9597

9698
while True:
9799
test_suite = None
@@ -142,7 +144,7 @@ def _run_test_suite(self, test_suite, write_stdout, num_tests):
142144

143145
try:
144146
if write_stdout:
145-
self._local.output = TeeToFile([sys.__stdout__, output_file])
147+
self._local.output = TeeToFile([self._stdout, output_file])
146148
else:
147149
self._local.output = TeeToFile([output_file])
148150

@@ -153,7 +155,7 @@ def _run_test_suite(self, test_suite, write_stdout, num_tests):
153155
traceback.print_exc()
154156
results = self._fail_suite(test_suite)
155157
finally:
156-
self._local.output = sys.__stdout__
158+
self._local.output = self._stdout
157159
output_file.flush()
158160
output_file.close()
159161

@@ -221,20 +223,21 @@ class ThreadLocalOutput(object):
221223
Replacement for stdout/err that separates re-directs
222224
output to a thread local file interface
223225
"""
224-
def __init__(self, local):
226+
def __init__(self, local, stdout):
225227
self._local = local
228+
self._stdout = stdout
226229

227230
def write(self, txt):
228231
if hasattr(self._local, "output"):
229232
self._local.output.write(txt)
230233
else:
231-
sys.__stdout__.write(txt)
234+
self._stdout.write(txt)
232235

233236
def flush(self):
234237
if hasattr(self._local, "output"):
235238
self._local.output.flush()
236239
else:
237-
sys.__stdout__.flush()
240+
self._stdout.flush()
238241

239242

240243
class TestScheduler(object):

0 commit comments

Comments
 (0)