class TestBaiduHttp(unittest.TestCase): URL = Config().get('URL') def setUp(self): self.client = HttpClient(url=self.URL, method='GET') def test_baidu_http(self): res = self.client.send() logger.debug(res.text) self.assertIn('百度一下', res.text)
class TestBaiduHttp(unittest.TestCase): URL = Config().get('baidu_url') def setUp(self): self.client = HttpClient(self.URL, method='GET') def test_index(self): response = self.client.send() assertionHttpCode(response, [200]) self.assertIn('百度一下,你就知道', response.text)
class TestWeather(unittest.TestCase): URL = Config().get('juhe_simple_weather') def setUp(self): self.client = HttpClient(url=self.URL, method='GET') #使用参数化测试接口,注意传入参数的格式 @parameterized.expand([['深圳', 'de5148ef8cabaf81c0d1c83c84098379', 1]]) def test_response(self, city, key, error_code): params = dict(city=city, key=key) response = self.client.send(params=params) # logger.info('响应结果%s'%(json.dumps(response.json(),ensure_ascii=False,indent=4))) cur_error_code = response.json()['error_code'] self.assertEqual(error_code, cur_error_code)
class JMESPathExtractor(object): """ 用JMESPath实现的抽取器 对于json格式数据实现简单方式抽取 """ def extract(self, query=None, body=None): try: return jmespath.search(query, json.loads(body)) except Exception as e: raise ValueError('Invalid query: ' + query + ":" + str(e)) if __name__ == '__main__': from utils.client import HttpClient res = HttpClient(url='http://wthrcdn.etouch.cn/weather_mini?citykey=101010100').send() print(res.text) # {"data": { # "yesterday": {"date": "17日星期四", "high": "高温 31℃", "fx": "东南风", "low": "低温 22℃", "fl": "<![CDATA[<3级]]>", # "type": "多云"}, # "city": "北京", # "aqi": "91", # "forecast": [ # {"date": "18日星期五", "high": "高温 28℃", "fengli": "<![CDATA[<3级]]>", "low": "低温 22℃", "fengxiang": "东北风", # "type": "多云"}, # {"date": "19日星期六", "high": "高温 29℃", "fengli": "<![CDATA[<3级]]>", "low": "低温 22℃", "fengxiang": "东风", # "type": "雷阵雨"}, # {"date": "20日星期天", "high": "高温 29℃", "fengli": "<![CDATA[<3级]]>", "low": "低温 23℃", "fengxiang": "东南风", # "type": "阴"}, # {"date": "21日星期一", "high": "高温 30℃", "fengli": "<![CDATA[<3级]]>", "low": "低温 24℃", "fengxiang": "西南风", # "type": "晴"},
def setUp(self): self.client = HttpClient(self.URL, method='GET')