I have broken my code down into separate functions for unit testing. I am trying to pick up Pytest testing framework and so far only know the basics.
I don't actually want to send a request to the internet -- this would be a fruitless test since I'd only be testing the urllib
library anyway. I do, however, want to test how the response is handled.
I have this function to make the request
def request_url(url):
return request.urlopen(url)
And then I am checking the content type:
def get_document_type(req):
""" checks if document is html or plain text """
doc_type = req.info().get_content_type()
if doc_type == "text/html":
return "html"
elif doc_type == "text/plain":
return "txt"
else:
return error_message["unsupported_document_type"]
Then I will need to test and I will need to mock each outcome. If this was Node.js I could use something like rewire or a sinon stub.
def get_content(req):
doc_type_response = get_document_type(req)
if doc_type_response == "html":
# handle html content
elif get_type_response == "txt":
# handle plain text
else:
return doc_type_response
This unit test works but I don't want to make the real call.
def test_request_url():
url = request_url(url_to_try).info().get_content_type()
assert url == "text/plain"
Does anybody know the best way to do this please?