hbutils.testing.isolated¶
- Overview:
Utilities for isolating environment, which can be using in testing.
isolated_directory¶
-
hbutils.testing.isolated.
isolated_directory
(mapping: Optional[Dict[str, str]] = None) → AbstractContextManager[source]¶ - Overview:
Do something in an isolated directory.
- Parameters:
mapping – Mappings for the isolated directory.
- Examples::
Simple usage
>>> import os >>> import pathlib >>> from hbutils.testing import isolated_directory >>> >>> with isolated_directory(): ... with open('file.txt', 'w') as f: ... print("Line 1", file=f) ... print("Line 2rd", file=f) ... print(os.listdir('.')) ... print(pathlib.Path('file.txt').read_text()) ['file.txt'] Line 1 Line 2rd >>> print(os.listdir('.')) ['hbutils', 'README.md', 'requirements.txt', ...]
Mapping files and directory inside
>>> import os >>> from hbutils.testing import isolated_directory >>> >>> with isolated_directory({ ... 'ts': 'hbutils/testing', ... 'README.md': 'README.md', ... }): ... print(os.listdir('.')) ... print(os.listdir('ts')) ['README.md', 'ts'] ['capture', 'generator', 'isolated', '__init__.py']
isolated_stdin¶
-
hbutils.testing.isolated.
isolated_stdin
(v: Union[str, List[str]], mem: bool = False)[source]¶ - Overview:
Isolation for stdin stream.
- Parameters:
v – Input content, a whole string or a list of string supported.
mem – Use memory or not. Default is
False
which means a temporary file will be used as fake input stream.
- Examples::
>>> from hbutils.testing import isolated_stdin >>> with isolated_stdin(['123', '456']): ... a = int(input()) ... b = int(input()) ... print(a, b, a + b) 123 456 579