I want to mock urllib.urlopen for creating unit test
def url_test(self):
response = urllib2.urlopen(test_url)
body = response.read()
if body:
return body.split(':')[0]
config.py
test_url = "localhost"
I want to mock the url_test() function but I do not understand how to mock the value of test_url. Because when I am trying to unit test the function it says me "connection refused"
this is what i tried.
@patch('urllib.urlopen')
def url_test(self, m_url):
m_response = m_url.return_value
m_response.read.return_value = 'Some body value:you wanted to return'
self.assertTrue(url_test(), "Failed")
m_url.assert_called_with('localhost')