简体   繁体   English

如何导入 pytest monkeypatch 插件?

[英]How do I import the pytest monkeypatch plugin?

I want to use the pytest monkeypatch plugin, but I can't figure out how to import it.我想使用pytestmonkeypatch插件,但我不知道如何导入它。 I've tried:我试过:

  • import monkeypath
  • import pytest.monkeypatch
  • from pytest import monkeypatch

It's not a plugin, it's a built-in pytest fixture .它不是插件,而是内置的pytest fixture

In a nutshell, that means you simply write a test with a monkeypatch argument, and the test will get the monkeypatch object as that argument.简而言之,这意味着您只需编写一个带有monkeypatch参数的测试,该测试将获取monkeypatch 对象作为该参数。

The page you linked has a simple example:您链接的页面有一个简单的示例:

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

Just to confirm 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 's answer)只是为了确认Eric Fulmer的评论,如果您碰巧想在 Python 中使用 MonkeyPatch,无论出于何种原因,它都对我有用(基于编译器的回答)

from _pytest.monkeypatch import MonkeyPatch

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

test_some_interaction(MonkeyPatch())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM