file.py: test.py: I always get an AssertionError. from the line assert x in GLOB_VAR Let me say that I DO need a global variable Answer It turned out that I shouldn’t have patched the global variable as I need to assert against the file’s global variable and not against the a mock’s instance global variable. This does does the trick:
Tag: python-unittest.mock
Using unittest.mock to mock a DRF response
My example is pretty basic: What I’m trying to do is mock the request to the /core/ URI so it returns a mocked response and not the response from the database. For example, considering unit testing in a CI pipeline when the database isn’t available. The below is what I have, but print(response.data) returns the actual response and not the
How to employ a MagicMock spec_set or spec on a method?
I am trying to use a method as a spec_set on a MagicMock. https://stackoverflow.com/a/25323517/11163122 provides a nice example of an unexpected call signature resulting in an Exception. Unfortunately, I can’t get it to work out for a method. How can I get the below code snippet to error out, given the calls don’t match the spec? I am using Python
Python mock – mocking class method that modifies class attributes
I currently have the following basic Python class that I want to test: As you can see, the step() method contains an expensive API call so I want to mock it with another function that avoids the expensive API call but still increments self.steps. I found that this is possible by doing this (as seen from here): I simply create
Python – How can I assert a mock object was not called with specific arguments?
I realize unittest.mock objects now have an assert_not_called method available, but what I’m looking for is an assert_not_called_with. Is there anything like that? I looked on Google and didn’t see anything, and when I tried just using mock_function.assert_not_called_with(…) it raised an AttributeError, meaning the function does not exist with that name. My current solution This works but clutters the code