Python Unittest for method returning json string -


i ma new writing python unit tests. have method in class returning json response api. json response contains attributes such data, token, object name , status. method hits api , returns response different values each time, can't hard code response , test it. how can write unit test type of method.

one thing, thought of check whether response not null. there other type of checks can here.

each time returns different token, date(timestamp). status same.

def json_get_status(self):     get_url = "xxxx" #url hit api     r = requests.get(get_url)     self.get_json = json.loads(r.text)     self.get_token=self.get_json["token"]     self.get_date=self.get_json["date"]     self.get_status=self.get_json["status"]     return self.get_json 

if method under test supposed "read status correctly", might want test that.

so assuming app like

def read_status(response):     parsed = json.loads(response)     # other stuff     return 

then in test_unit.py

def test_read_status(self):    mock_input_val = {'something': 'good val'}    expected_positive_return_val =    self.assertequal(read_status(json.dumps(mock_input_val)),                            expected_positive_return_val) 

also negative test condition read_status either fails parse json object or finds error in response.


Comments

Popular posts from this blog

css - SVG using textPath a symbol not rendering in Firefox -

Java 8 + Maven Javadoc plugin: Error fetching URL -

datatable - Matlab struct computations -