I’m trying to understand the different ways to patch a constant in Python using mock.patch. My goal is to be able to use a variable defined in my Test class as the patching value for my constant. I’ve found this question which explains how to patch a constant: How to patch a constant in python And this question which explains
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
Mock timing in a context to create models with a field DateTimeField with auto_now_add=True
I’d like to mock timing so that be able to set certain time to a field of type DateTimeField with auto_now_add=True during my tests e.g: I’m aware the current date is always used for this type of fields, that is why I’m looking for an alternative to mock somehow the system timing, but just in a context. I’ve tried some
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
Python unit test mock, get mocked function’s input arguments
I want a unit test to assert that a variable action within a function is getting set to its expected value, the only time this variable is used is when it is passed in a call to a library. My thought was that I could mock lib.event.Event, and get its input arguments and assert they are of specific value. >Is
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()