From 9a75ff8e6a25a3465132b0de0c20cb2b897ef7c2 Mon Sep 17 00:00:00 2001 From: Michael Stewart Date: Wed, 24 Sep 2025 00:54:34 -0400 Subject: [PATCH] not sure if this is sufficient or if we want to be able to verify the sequence of prompts... https://chatgpt.com/share/68d37918-0000-800f-832b-981fcaf9da7a --- CHANGELOG.md | 2 +- jmu_pytest_utils/fixtures.py | 23 +++++++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) create mode 100644 jmu_pytest_utils/fixtures.py diff --git a/CHANGELOG.md b/CHANGELOG.md index 4ab3fcf..8821f91 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,7 +8,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] -- TBD +- add `jmu_pytest_utils.fixtures.observable_stdin`: without something like this, assignments that wish to test the students' messages prompting the user to provide input would have to specify that they must not use the `input()` function's `prompt` parameter because [when run with output redirection (as in testing with `capsys`)), rather than connected to a tty, the input prompts go to stderr](discuss.python.org/t/builtin-function-input-writes-its-prompt-to-sys-stderr-and-not-to-sys-stdout/12955). ## [1.3.0] - 2025-09-17 diff --git a/jmu_pytest_utils/fixtures.py b/jmu_pytest_utils/fixtures.py new file mode 100644 index 0000000..c99aa97 --- /dev/null +++ b/jmu_pytest_utils/fixtures.py @@ -0,0 +1,23 @@ +"""Common test fixtures used in many autograders.""" + +from pytest import fixture + + +@fixture +def observable_stdin(monkeypatch, capsys): + """ + Fixture that replaces builtins.input so prompts are printed + (capturable by capsys) and test inputs can be fed in. + """ + + def _make_observable_stdin(responses): + responses_iter = iter(responses) + + def fake_input(prompt=""): + # mimic real input(): print the prompt + print(prompt, end="") + return next(responses_iter) + + monkeypatch.setattr("builtins.input", fake_input) + + return _make_observable_stdin