Thanks to visit codestin.com
Credit goes to github.com

Skip to content

pytester (?) causes monkeypatch to not tear down correctly #9708

@asottile

Description

@asottile

have a difficult time tracking down the "why" for this but I've narrowed it down to this minimal testcase:

import os

import pytest


def test_a(monkeypatch, pytester):  # removing `pytester` here causes the session fixture to "pass"
    monkeypatch.setenv("PY_COLORS", "1")

@pytest.fixture(autouse=True, scope='session')
def t():
    yield
    assert 'PY_COLORS' not in os.environ
$ pytest -p pytester t.py
============================= test session starts ==============================
platform linux -- Python 3.8.10, pytest-7.1.0.dev230+g3b549642e, pluggy-1.0.0
rootdir: /tmp/pytest, configfile: pyproject.toml
plugins: hypothesis-6.37.2
collected 1 item                                                               

t.py .E                                                                  [100%]

==================================== ERRORS ====================================
_________________________ ERROR at teardown of test_a __________________________

    @pytest.fixture(autouse=True, scope='session')
    def t():
        yield
>       assert 'PY_COLORS' not in os.environ
E       AssertionError: assert 'PY_COLORS' not in environ({'SHELL': '/bin/bash', 'SESSION_MANAGER': 'local/babibox:@/tmp/.ICE-unix/1733,unix/babibox:/tmp/.ICE-unix/1733...st-pollution', '_': '/tmp/pytest/venv/bin/pytest', 'PYTEST_CURRENT_TEST': 't.py::test_a (teardown)', 'PY_COLORS': '0'})
E        +  where environ({'SHELL': '/bin/bash', 'SESSION_MANAGER': 'local/babibox:@/tmp/.ICE-unix/1733,unix/babibox:/tmp/.ICE-unix/1733...st-pollution', '_': '/tmp/pytest/venv/bin/pytest', 'PYTEST_CURRENT_TEST': 't.py::test_a (teardown)', 'PY_COLORS': '0'}) = os.environ

t.py:12: AssertionError
=========================== short test summary info ============================
ERROR t.py::test_a - AssertionError: assert 'PY_COLORS' not in environ({'SHEL...
========================== 1 passed, 1 error in 0.18s ==========================

in our self tests this can be reproduced if you can run these tests in order (I can't figure out a definitive way to force that using just pytest (pytest seems to want to reorder them even when they're passed in an explicit order on the commandline) -- but my test pollution finder is able to do this, and I suspect pytest-select can too):

testing/test_terminal.py::TestTerminal::test_report_teststatus_explicit_markup
testing/io/test_terminalwriter.py::test_should_do_markup_FORCE_COLOR
$ pytest -s -v -p detect_test_pollution --dtp-testids-input-file /dev/stdin testing <<< $'testing/test_terminal.py::TestTerminal::test_report_teststatus_explicit_markup\ntesting/io/test_terminalwriter.py::test_should_do_markup_FORCE_COLOR'
============================= test session starts ==============================
platform linux -- Python 3.8.10, pytest-7.1.0.dev230+g3b549642e, pluggy-1.0.0 -- /tmp/pytest/venv/bin/python
cachedir: .pytest_cache
hypothesis profile 'default' -> database=DirectoryBasedExampleDatabase('/tmp/pytest/.hypothesis/examples')
rootdir: /tmp/pytest, configfile: pyproject.toml, testpaths: testing
plugins: hypothesis-6.37.2
collected 3140 items                                                           

testing/test_terminal.py::TestTerminal::test_report_teststatus_explicit_markup ============================= test session starts ==============================
platform linux -- Python 3.8.10, pytest-7.1.0.dev230+g3b549642e, pluggy-1.0.0 -- /tmp/pytest/venv/bin/python
cachedir: .pytest_cache
rootdir: /tmp/pytest-of-asottile/pytest-272/test_report_teststatus_explicit_markup0
collecting ... collected 1 item

test_report_teststatus_explicit_markup.py::test_foobar FOO               [100%]
test_report_teststatus_explicit_markup.py::test_foobar FOO               [100%]
test_report_teststatus_explicit_markup.py::test_foobar FOO               [100%]

================================ 3 foo in 0.00s ================================
PASSED
testing/io/test_terminalwriter.py::test_should_do_markup_FORCE_COLOR FAILED

=================================== FAILURES ===================================
______________________ test_should_do_markup_FORCE_COLOR _______________________

monkeypatch = <_pytest.monkeypatch.MonkeyPatch object at 0x7fae39208520>

    def test_should_do_markup_FORCE_COLOR(monkeypatch: MonkeyPatch) -> None:
        monkeypatch.setitem(os.environ, "FORCE_COLOR", "1")
>       assert_color_set()

testing/io/test_terminalwriter.py:205: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

    def assert_color_set():
        file = io.StringIO()
        tw = terminalwriter.TerminalWriter(file)
>       assert tw.hasmarkup
E       assert False
E        +  where False = <_pytest._io.terminalwriter.TerminalWriter object at 0x7fae39199460>.hasmarkup

testing/io/test_terminalwriter.py:170: AssertionError
=========================== short test summary info ============================
FAILED testing/io/test_terminalwriter.py::test_should_do_markup_FORCE_COLOR
========================= 1 failed, 1 passed in 1.42s ==========================

Metadata

Metadata

Assignees

No one assigned

    Labels

    topic: fixturesanything involving fixtures directly or indirectlytype: bugproblem that needs to be addressed

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions