jeudi 31 décembre 2015

Use mock to patch sleep in unit test

I have a piece of code sitting in a module like this:

MyWorker.py:

from gevent import sleep

class MyWorker(object):
  def run(self):
    for _ in range(10):
      do_something()
      sleep(1)

Then I want to test this while mocking sleep. I tried multiple ways of doing it but all failed. One way I thought should be working:

from mock import patch
from x.y.z.MyWorker import MyWorker

class TestWorker(unitest.testCase):
  def Test_my_worker(self):
    with patch('x.y.z.MyWorker.sleep'):
      a = MyWorker()
      a.run()

Aucun commentaire:

Enregistrer un commentaire