如何导入 pytest monkeypatch 插件?

How do I import the pytest monkeypatch plugin?

我想使用 pytest monkeypatch 插件,但不知道如何导入它。我试过:

这不是插件,它是内置的pytest fixture

简而言之,这意味着您只需编写一个带有 monkeypatch 参数的测试,测试就会将 monkeypatch 对象作为该参数。

您链接的页面有一个简单的例子:

def test_some_interaction(monkeypatch):
    monkeypatch.setattr("os.getcwd", lambda: "/")

只是为了确认 Eric Fulmer's comment, if you do happen to want to use MonkeyPatch from within Python for whatever reason, it worked for me like this (based on The Compiler 的回答)

from _pytest.monkeypatch import MonkeyPatch

def test_some_interaction(monkeypatch):
    monkeypatch.setattr("os.getcwd", lambda: "/")

test_some_interaction(MonkeyPatch())