Coverage for torxtools/testtools.py: 0%
19 statements
« prev ^ index » next coverage.py v7.5.1, created at 2024-05-18 01:02 +0000
« prev ^ index » next coverage.py v7.5.1, created at 2024-05-18 01:02 +0000
1"""
2Functions for working with unit tests.
4"""
5import contextlib
6import sys
7import typing as t
9__all__: t.List[str] = [
10 "disable_outputs",
11]
14@contextlib.contextmanager
15def disable_outputs():
16 """
17 stack overflow programming
18 https://stackoverflow.com/questions/1809958/hide-stderr-output-in-unit-tests
19 """
20 prev_stdout = sys.stdout
21 prev_stderr = sys.stderr
23 class DevNull:
24 def write(self, _):
25 pass
27 def flush(self):
28 pass
30 sys.stdout = DevNull()
31 sys.stderr = DevNull()
32 try:
33 yield
34 finally:
35 sys.stdout = prev_stdout
36 sys.stderr = prev_stderr