Próbuję wyśmiewać klasy narzędzie (w tym przypadku narzędzie Python Logger) w teście jednostki.Jak używać monkeypatch w metodzie "setup" do testów jednostkowych przy użyciu pytest?
Podczas gdy wiem, jak to zrobić za pomocą monkeypatch na poziomie testu, miałem nadzieję, że mogę po prostu zrobić to w ramach konfiguracji/globalnie w jakiś sposób.
Oto, co mam nadzieję, że mogę to zrobić (ale ja dostaję błędy):
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)
Jaki jest właściwy sposób to zrobić?
Edycja: Przykład błędu
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'
Proszę podać błędy, które otrzymujesz, to naprawdę pomaga nam zrozumieć, co dzieje się nie tak. –