Skip to content
Advertisement

Tag: mocking

Mock an entire module in python

I have an application that imports a module from PyPI. I want to write unittests for that application’s source code, but I do not want to use the module from PyPI in those tests. I want to mock it entirely (the testing machine will not contain that PyPI module, so any import will fail). Currently, each time I try to

How to mock imported library methods with unittest.mock in Python 3.5?

Is it possible to mock methods of imported modules with unittest.mock in Python 3.5? I expected that the the os.listdir method returns the specified side_effect “a” on the first call, but inside of my_function the unpatched os.listdir is called. Answer unittest.mock have two main duties: Define Mock objects: object designed to follow your screenplay and record every access to your

How can I mock requests and the response?

I am trying to use Pythons mock package to mock Pythons requests module. What are the basic calls to get me working in below scenario? In my views.py, I have a function that makes variety of requests.get() calls with different response each time In my test class I want to do something like this but cannot figure out exact method

Trying to mock datetime.date.today(), but not working

Can anyone tell me why this isn’t working? Perhaps someone could suggest a better way? Answer There are a few problems. First of all, the way you’re using mock.patch isn’t quite right. When used as a decorator, it replaces the given function/class (in this case, datetime.date.today) with a Mock object only within the decorated function. So, only within your today()

Advertisement