2017-08-25 3 views
2

Wenn ich Mock-Bibliothek, z. with mock.patch('os.path.join'): alles funktioniert, aber wenn ich pytest-Mock wie diese mocker.patch('os.path.join') verwende ich die folgende Fehlermeldung erhalten, wenn Behauptung fehlschlägt:Kann 'os.path.join' nicht mit Pytest-Mock verspotten

Es scheint, dass pytest ‚os.path‘ Modul zu verwenden versucht, aber wie es mit Spötter geflickt, es scheitert und löst den Fehler aus, mache ich etwas falsch?

AssertionError: Expected 'transform_file' to be called once. Called 0 times. 

During handling of the above exception, another exception occurred: 

Traceback (most recent call last): 
    File "<frozen importlib._bootstrap>", line 890, in _find_spec 
AttributeError: 'AssertionRewritingHook' object has no attribute 'find_spec' 

During handling of the above exception, another exception occurred: 

Traceback (most recent call last): 
    File "c:\anaconda\lib\site-packages\py\_path\common.py", line 29, in fspath 
    return path_type.__fspath__(path) 
AttributeError: type object 'MagicMock' has no attribute '__fspath__' 

During handling of the above exception, another exception occurred: 

Traceback (most recent call last): 
    File "c:\anaconda\lib\site-packages\py\_path\local.py", line 152, in __init__ 
    path = fspath(path) 
    File "c:\anaconda\lib\site-packages\py\_path\common.py", line 42, in fspath 
    + path_type.__name__) 
TypeError: expected str, bytes or os.PathLike object, not MagicMock 
... etc etc 

Antwort

0

zu verspotten oder besser gesagt Testeinheit os.path.join Sie monkeypatch verwenden könnte, da Sie bereits verwenden py.test beispiels source

# content of test_module.py 
import os.path 
def getssh(): # pseudo application code 
    return os.path.join(os.path.expanduser("~admin"), '.ssh') 

def test_mytest(monkeypatch): 
    def mockreturn(path): 
     return '/abc' 
    monkeypatch.setattr(os.path, 'expanduser', mockreturn) 
    x = getssh() 
    assert x == '/abc/.ssh'