I had problems with the Python layout and went crazy. I dwelled on this issue for fear of voting for insufficient research. I have a cumulative 24 hours in the last week, trying to figure out how to get this job, and cannot.
I read many examples and created this from them. I know that fake objects should be easy to use, but it took too much time. Now I have no time.
I am trying to do two simple things here:
1. Override request.ok status code inside another function
2. Reason for throwing urllib2.HTTPError to throw
I completed these two tasks as a simple example for your convenience:
#ExampleModule.py import requests import urllib2 def hello_world(): try: print "BEGIN TRY" r = requests.request('GET', "http://127.0.0.1:80") print r.ok if r.ok: print "PATCH 1 FAILED" else: print "PATCH 1 SUCCESSFUL" except urllib2.HTTPError: print "PATCH 2 SUCCESSFUL" print "EXCEPTION 2 HIT\n" else: print "PATCH 2 FAILED\n"
and
#in TestModule.py import mock import ExampleModule def test_function_try(): with mock.patch('ExampleModule.hello_world') as patched_request: patched_request.requests.request.ok = False result = ExampleModule.hello_world() print result def test_function_exception(): with mock.patch('ExampleModule.hello_world') as patched_exception: patched_exception.urllib2.side_effect = HTTPError result = ExampleModule.hello_world() print result test_function_try() test_function_exception()
The normal exit call hello_world ():
BEGIN TRY True <Response [200]>
Normal call for test_function_try () results:
<MagicMock name='hello_world()' id='70272816'> #From the "print result" inside test_function_try()
The normal call to the output of test_function_exception ():
<MagicMock name='hello_world()' id='62320016'> #From the "print result" inside test_function_exception()
Obviously, in fact, I am not returning anything from hello_world (), so it looks like a fixed object - this is the hello_world () function instead of queries or fixed urllib2 modules.
It should be noted that when I try to install a patch using 'ExampleModule.hello_world.requests' or 'ExampleModule.hello_world.urllib2', I get an error message: they cannot be found in hello_world ()
SUMMARY OF THE QUESTION
- What is wrong with the two functions test_function_try () and test_function_exception ()? What needs to be changed so that I can manually assign the request.ok value inside hello_world (), as well as manually raise an HTTPError exception so that I can check the code in this block ... Bonus points for explaining "when" exactly get an exception: as soon as attempt was made: or when the request is called or some other time?
- Something that bothered me: would my print statements inside ExampleModule.py show if my fixes and test layouts were working, or should I use assert methods to get the truth? I'm not sure if a statement is necessary when people mention "use statements to find out if the actual corrected object was called, etc." or if it is for convenience / consent / practicality.
UPDATE
After changing the patch target to the request.request () function, as suggested by @chepner, I get the following output:
BEGIN TRY False PATCH 1 SUCCESSFUL PATCH 2 FAILED BEGIN TRY Traceback (most recent call last): File "C:\LOCAL\ECLIPSE PROJECTS\MockingTest\TestModule.py", line 44, in <module> test_function_exception() File "C:\LOCAL\ECLIPSE PROJECTS\MockingTest\TestModule.py", line 19, in test_function_exception ExampleModule.hello_world() File "C:\LOCAL\ECLIPSE PROJECTS\MockingTest\ExampleModule.py", line 12, in hello_world r = requests.request('GET', "http://127.0.0.1:8080") File "C:\Python27\lib\site-packages\mock\mock.py", line 1062, in __call__ return _mock_self._mock_call(*args, **kwargs) File "C:\Python27\lib\site-packages\mock\mock.py", line 1118, in _mock_call raise effect TypeError: __init__() takes exactly 6 arguments (1 given)