hbutils.testing.isolated.input
Isolation utilities for standard input streams in tests.
This module provides utilities for isolating and mocking standard input (stdin) during testing or other scenarios where controlled input is needed. It allows you to simulate user input by providing predefined strings or lists of strings as stdin content. The implementation supports both in-memory and file-based stdin simulation.
The module contains the following main components:
isolated_stdin()- Context manager for redirectingstdinto custom input
Example:
>>> from hbutils.testing.isolated.input import isolated_stdin
>>> with isolated_stdin(['123', '456']):
... a = int(input())
... b = int(input())
... print(a, b, a + b)
123 456 579
>>> with isolated_stdin('single line input', mem=True):
... line = input()
... print(f'Read: {line}')
Read: single line input
__all__
- hbutils.testing.isolated.input.__all__ = ['isolated_stdin']
Built-in mutable sequence.
If no argument is given, the constructor creates a new empty list. The argument must be an iterable if specified.
isolated_stdin
- hbutils.testing.isolated.input.isolated_stdin(v: str | List[str], mem: bool = False) Iterator[None][source]
Isolation for stdin stream.
This context manager allows you to mock stdin with predefined input content, useful for testing interactive programs or simulating user input.
- Parameters:
v (Union[str, List[str]]) – Input content, either a whole string or a list of strings. If a list is provided, each element will be treated as a separate line.
mem (bool) – Use memory-based (StringIO) or file-based approach for stdin. Default is
Falsewhich means a temporary file will be used as a fake input stream. Set toTrueto use in-memoryio.StringIOinstead.
- Returns:
A context manager for isolated stdin.
- Return type:
Iterator[None]
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 >>> with isolated_stdin('single line input', mem=True): ... line = input() ... print(f'Read: {line}') Read: single line input