Proxy configuration of a machine can be easily fetched using
def check_proxy():
import urllib2
http_proxy = urllib2.getproxies().get('http')
I need to write a test for the above written function. In order to do that I need to:-
- Set the system-wide proxy to an
invalid URL during the test(sounds
like a bad idea). - Supply an invalid
URL to http_proxy.
How can I achieve either of the above?
Looking at the question tags I see you want to write unit-tests for the function. And where is your unit here? Where is your business logic?
getproxiesandgetare functions of the standard Python library. You shouldn’t test others’ code in your unit-tests. Furthermore it enough to test only Things That Could Possibly Break.If I’ve misunderstood your question and indeed your function looks like this:
and you don’t know how to test the “complex” code due to dependency on proxy, I recommend to split the function in two:
Now you’re able to test
_check_proxyalone, using any mock/stub you specially prepared for the role ofhttp_proxy. Write few tests for_check_proxyand leave originalcheck_proxyuntested (in sense of unit-tests). It is OK.