ENH: Make monkeypatch available
Closed this issue · 3 comments
Not sure if this is possible, but for some tests I need to monkeypatch sys calls to force test unity on different systems, like so:
# setup
def mockreturn(host):
return 'test-machine'
from iuvs import io
from pathlib import Path
def test_get_data_path(monkeypatch):
monkeypatch.setattr(socket, 'gethostname', mockreturn)
x = io.get_stage_path()
assert x == Path('/abc')
The test works when run in the terminal, but I really would like to be able to have my tests working in the notebook. Is there a way to have this working? Like this it of course fails, because I don't have a monkeypatch
object available during runtime of the notebook. I already asked Holger Krekel (pytest author) what he thinks.
Hm, interesting, seems for simple things like this, one does not need monkeypatch
, can just assign myself a new function to a module attribute like so:
def mockreturn():
return 'test-machine'
import socket
socket.gethostname = mockreturn
from iuvs import io
from pathlib import Path
# test_get_stage_path():
x = io.get_stage_path()
assert x == Path('/abc')
Works just as well, so maybe we won't need this. Closing.
To get this, you probably should see the function itself, ;)
def get_stage_path():
host = socket.gethostname()
if host.startswith('maven-iuvs-itf'):
stage = Path('/maven_iuvs/stage/products')
elif host.startswith('test-machine'):
stage = Path('/abc')
else:
stage = Path(os.environ['HOME']) / 'data' / 'iuvs'
return stage
And for this function, this does not make much sense, admittedly, but I want to test other functions that use this as a start to define the basic path.