Python is a relatively new language for me. Unit testing and dependency on injections is what I have been doing for some time, so I am familiar with it from the point of view of C #.
I recently wrote this snippet of Python code:
import requests # my dependency: http:
And then I realized that I had just created a hard-coded dependency. Bleh.
I thought about changing my code to inject constructor dependencies:
def __init__(self,requestLib=requests): self.__request = requestLib def __do(self, url, datagram): return self.__request.post(self, url, datagram)
Now this allows me to introduce a fake / mock dependency for the sake of Unit Testing, but was not sure if it was considered Python-ic. So I turn to the Python community for advice.
What are some examples of Python-ic ways to make basic DI (mainly for writing Unit tests that use Mocks / Fake)?
APPENDIX For anyone interested in the layout answer, I decided to ask a separate question here: How does @ mock.patch know which parameter to use for each layout of the object?
python dependency-injection unit-testing mocking
Pretzel
source share