Created
May 17, 2024 15:03
-
-
Save chapmanjacobd/09c5057b2c06de366206660ef4137c66 to your computer and use it in GitHub Desktop.
restore stdin on error for pytest pdb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
import pytest | |
from io import StringIO | |
class MockStdin: | |
def __init__(self, input_text): | |
self.input_text = input_text | |
self.original_stdin = None | |
def __enter__(self): | |
self.original_stdin = sys.stdin | |
sys.stdin = StringIO(self.input_text) | |
def __exit__(self, exc_type, exc_val, exc_tb): | |
sys.stdin = self.original_stdin | |
@pytest.fixture | |
def mock_stdin(): | |
def _mock_stdin(input_text): | |
return MockStdin(input_text) | |
return _mock_stdin |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment