How to use monkeypatch in a “setup” method for unit tests using pytest?

守給你的承諾、 提交于 2020-07-18 05:08:50

问题


I am attempting to mock out a utility class (In this case the python logger utility) in a unit test.

While I know how to do it using monkeypatch on a per test level, I was hoping I could simply do it as part of the setup/globally in some way.

Here is what I am hoping I can do (but I am getting errors):

import logging

...

def setup(self, monkeypatch):

    class fake_logger(level):
        def __init__(self, val):
            pass

        def setLevel(self, level):
            # Do something

    def mock_logger(level):
        return fake_logger(level)
    monkeypatch.setattr(logging, 'getLogger', mock_logger)

What is the right way to do this?

EDIT: Example error

name = 'setup'

def call_optional(obj, name):
    method = getattr(obj, name, None)
    isfixture = hasattr(method, "_pytestfixturefunction")
    if method is not None and not isfixture and py.builtin.callable(method):
        # If there's any problems allow the exception to raise rather than
        # silently ignoring them
>           method()
E           TypeError: setup() missing 1 required positional argument: 'monkeypatch'

回答1:


monkeypatch works as a normal pytest fixture. If you want to use it, you need to make your method as a fixture as well.

import logging

import pytest


@pytest.fixture
def setup(monkeypatch):

    class fake_logger(object):
        def __init__(self, val):
            pass

        def setLevel(self, level):
            # Do something
            pass

    def mock_logger(level):
        return fake_logger(level)
    monkeypatch.setattr(logging, 'getLogger', mock_logger)

def test_fake_logger(setup):
    # test steps

and if you check the type of logging.getLogger('any level') in test, it will be fake_logger you defined.



来源:https://stackoverflow.com/questions/37948345/how-to-use-monkeypatch-in-a-setup-method-for-unit-tests-using-pytest

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!