New issue 603: monkeypatch does not work on already-imported function https://bitbucket.org/hpk42/pytest/issue/603/monkeypatch-does-not-work-on-already
Marco Chomut: Given the following tests file: ``` #!python from project.main import foo def test_foo(monkeypatch): monkeypatch.setattr('project.bar.zoo', lambda x: 0) assert foo() == 0 ``` And this system under test: ``` #!python # project/main.py from project.bar import zoo def foo(): return zoo('lion') ``` ``` #!python # project/bar.py def zoo(animal): if animal == 'lion': return 7 ``` Then the ``zoo`` that's already been imported in ``main.py`` remains unpatched. Moving the imports around to be local to the test function would fix the problem, unless ``main.py`` is imported by other files at any point during the test suite running. Alternatively, if ``main.py`` instead does ``import project.bar`` and calls its dependency with ``project.bar.zoo('lion')``, then the test passes again. But this is cumbersome and difficult to enforce across an entire project. _______________________________________________ pytest-commit mailing list pytest-commit@python.org https://mail.python.org/mailman/listinfo/pytest-commit