Skip to content

Instantly share code, notes, and snippets.

@chapmanjacobd
Created May 17, 2024 15:03
Show Gist options
  • Save chapmanjacobd/09c5057b2c06de366206660ef4137c66 to your computer and use it in GitHub Desktop.
Save chapmanjacobd/09c5057b2c06de366206660ef4137c66 to your computer and use it in GitHub Desktop.
restore stdin on error for pytest pdb
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