8

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'
asked Jun 21, 2016 at 15:17
1
  • Please include the errors you are getting, it really helps us understand what is going wrong. Commented Jun 21, 2016 at 16:37

1 Answer 1

8

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.

answered Jun 21, 2016 at 23:57
Sign up to request clarification or add additional context in comments.

1 Comment

You can also do @pytest.fixture(autouse=True) so it's automatically applied for all tests without having to have a setup argument.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.