Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I import the pytest monkeypatch plugin?

Tags:

I want to use the pytest monkeypatch plugin, but I can't figure out how to import it. I've tried:

  • import monkeypath
  • import pytest.monkeypatch
  • from pytest import monkeypatch
like image 800
John Bachir Avatar asked May 12 '16 17:05

John Bachir


People also ask

Is monkeypatch part of Pytest?

monkeypatch is a part of the pytest-mock library that allows you to intercept what a function would normally do, substituting its full execution with a return value of your own specification.

What is monkeypatch Pytest?

The monkeypatch fixture helps you to safely set/delete an attribute, dictionary item or environment variable, or to modify sys. path for importing. The monkeypatch fixture provides these helper methods for safely patching and mocking functionality in tests: monkeypatch.


1 Answers

It's not a plugin, it's a built-in 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.

The page you linked has a simple example:

def test_some_interaction(monkeypatch):
    monkeypatch.setattr("os.getcwd", lambda: "/")
like image 156
The Compiler Avatar answered Sep 20 '22 11:09

The Compiler