Skip to content

Setting COLUMN environment variable using monkeypatch messes up output of run results #11204

Closed Answered by nicoddemus
dgard1981 asked this question in General

You must be logged in to vote

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?

Yeah you will need to limit the scope where COLUMNS is set, so I suggest to write your own context manager:

@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): 
        ...

Replies: 1 comment 1 reply

You must be logged in to vote
1 reply
@dgard1981

Answer selected by dgard1981
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants