Less-Tabs
Less-Tabs

Reputation: 1

Mock urlilb+json in Python

looking for a way to correctly mock this piece of code:

import json
from urllib import urlopen

a_url = "http://blabla"
my_response= urlopen(a_url)
a_dict = json.loads(my_response.read())

It reads from an URL, then the response uses json.loads to store everything in a dict. Ideas?

Upvotes: 0

Views: 73

Answers (1)

Alex Hall
Alex Hall

Reputation: 36023

urlopen should return a mock object where .read() returns a string containing JSON, e.g. '{"a": "b"}'.

Then throw it all away and use requests.

Upvotes: 1

Related Questions