Setting COLUMN
environment variable using monkeypatch
messes up output of run results
#11204
-
I have the need to capture output at a standard width. One option is to use @pytest.fixture(scope='function')
def mock_width(monkeypatch)
monkeypatch.setenv('COLUMNS', '120') I was hoping that I would be able to use @pytest.fixture(scope='function')
def mock_width(monkeypatch) -> None:
monkeypatch.setenv('COLUMNS', '120')
yield
monkeypatch.delenv('COLUMNS') Is my only option to use monkeypatch.setenv() at the beginning of every test function that tests terminal output, and monkeypatch.delenv() at the end. Or is there a better way? Thanks. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Yeah you will need to limit the scope where @contextmanager
def using_column_width(w: int):
mp = pytest.MonkeyPatch()
mp.setenv("COLUMNS", w)
try:
yield
finally:
mp.undo()
def test_foo():
with using_column_width(120):
... |
Beta Was this translation helpful? Give feedback.
Yeah you will need to limit the scope where
COLUMNS
is set, so I suggest to write your own context manager: