Sto tentando di prendere in giro una classe di utilità (in questo caso l'utilità di registrazione python) in un test di unità.Come utilizzare monkeypatch in un metodo "setup" per i test unitari usando pytest?
Mentre so come farlo utilizzando monkeypatch a livello di test, speravo di poterlo fare semplicemente come parte del setup/globalmente in qualche modo.
Ecco quello che spero che posso fare (ma sto ottenendo gli errori):
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)
Qual è il modo giusto per fare questo?
EDIT: errore Esempio
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'
Si prega di includere gli errori che si stanno ottenendo, ci aiuta davvero a capire cosa sta andando male. –