Coverage for torxtools/testtools.py: 0%

19 statements  

« prev     ^ index     » next       coverage.py v7.6.9, created at 2024-12-20 22:02 +0000

1""" 

2Functions for working with unit tests. 

3 

4""" 

5 

6import contextlib 

7import sys 

8import typing as t 

9 

10__all__: t.List[str] = [ 

11 "disable_outputs", 

12] 

13 

14 

15@contextlib.contextmanager 

16def disable_outputs(): 

17 """ 

18 stack overflow programming 

19 https://stackoverflow.com/questions/1809958/hide-stderr-output-in-unit-tests 

20 """ 

21 prev_stdout = sys.stdout 

22 prev_stderr = sys.stderr 

23 

24 class DevNull: 

25 def write(self, _): 

26 pass 

27 

28 def flush(self): 

29 pass 

30 

31 sys.stdout = DevNull() 

32 sys.stderr = DevNull() 

33 try: 

34 yield 

35 finally: 

36 sys.stdout = prev_stdout 

37 sys.stderr = prev_stderr