mercredi 19 juillet 2017

Stub python time method with mockito (when)

In the code, I'm using the python method time():

from time import time

# Some code
t=time()
# Some more code

main.py

In my tests, I want to stub the time method with mockito, to return always the same value:

import time
#...
when(time).time().thenReturn(2)
#...

test.py

However, that doesn't work unless I change how I call the time method in main:

import time
t=time.time()

main_2.py

I would like to avoid changing the main code, or at least understand why that change is needed in order for the stub to work.

Aucun commentaire:

Enregistrer un commentaire