dimanche 3 juillet 2016

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


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?


Aucun commentaire:

Enregistrer un commentaire