接口自动化实例--聚合数据天气接口(接口关联)
接口地址:https://www.juhe.cn/docs/api/id/73
接口1:http://apis.juhe.cn/simpleWeather/cityList 获取支持的城市列表
接口2:http://apis.juhe.cn/simpleWeather/query 根据城市来查询该城市天气
从支持的城市列表接口:http://apis.juhe.cn/simpleWeather/cityList 中取数据。
来替换 xls测试用例中的变量:"city":"${city}",进行该城市天气的查询
注意:
self.city_name_pattern = r"\${city}" 模式化字符串必须带 \。否则使用search函数,查找失败。
return之后后面的代码可能并不会执行
代码:
import unittest import re from libs.ddt import ddt, data from scripts.handle_excel import HandleExcel from scripts.handle_config import do_config from scripts.handle_log import do_logger from scripts.constants import TEST_DATAS_FILES_PATH from scripts.handle_request import HandleRequest do_excel = HandleExcel(TEST_DATAS_FILES_PATH, "weatherforecast") cases = do_excel.get_cases() @ddt class TestWeatherForecast(unittest.TestCase): @classmethod def setUpClass(cls): """ 在所有用例执行之前, 会调用一次setUpClass类属性 :return: """ cls.do_request = HandleRequest() do_logger.info("{:=^40s}".format("开始执行用例")) @classmethod def tearDownClass(cls): """ 在所有用例执行之后, 会调用一次tearDownClass类属性 :return: """ cls.do_request.close() do_logger.info("{:=^40s}".format("执行用例结束")) def test_getcity(self): ''' 从获取支持的城市列表接口中取出一个城市 :return: ''' url = do_config.get_value('api', 'prefix') + '/simpleWeather/cityList' # print(url) data = {"key":"e3cf8a6c0b39184a12c45df6ae409679"} res = self.do_request.to_request(url=url, method="get", data=data) # print(res) # print(res.text) result = res.json() # print(res.json()) # print(type(res.json())) # print(result["reason"]) try: self.assertIn("查询成功", result["reason"]) ##提取数据 city = result["result"][1]["district"] print(city) return city do_logger.info("从支持的城市中查询城市为: {}\n".format(city)) except Exception as e: do_logger.error("查询支持的城市出错\n具体异常为: {}\n".format(e)) raise e @data(*cases) # 拆包 def test_search_weather(self, one_case): """ 从取出的xls中,进行查询,是否带有变量${city},带有则进行替换为test_getcity的城市 :return: """ # 将表格中的 ${city} ,从 test_getcity方法 响应中提取 # r"\${city}" 必须带 \ 进行转义 self.city_name_pattern = r"\${city}" if re.search(self.city_name_pattern, one_case['data']): # print(self.city_name_pattern) # print(self.one_case['data']) get_city = self.test_getcity() one_case['data'] = re.sub(self.city_name_pattern, get_city, one_case['data']) # print(one_case['data']) # return one_case['data'] # print(one_case['data']) else: # return one_case['data'] print("未匹配") # 拼接完整的url new_url = do_config.get_value('api', 'prefix') + one_case['url'] print(new_url) # 向服务器发起请求 res = self.do_request.to_request(url=new_url, method=one_case['method'], data=one_case['data']) # print(res) # print(res.text) expect_result = one_case['expected'] msg = one_case['title'] success_msg = do_config.get_value("msg", "success_result") fail_msg = do_config.get_value("msg", "fail_result") try: self.assertIn(expect_result, res.text, msg=msg) do_logger.info("{}, 执行的结果为: {}\n".format(msg, success_msg)) do_excel.write_result(one_case['case_id'] + 1, res.text, success_msg) except AssertionError as e: do_logger.error("{}, 执行的结果为: {}\n具体异常为: {}\n".format(msg, fail_msg, e)) do_excel.write_result(one_case['case_id'] + 1, res.text, fail_msg) raise e if __name__ == '__main__': # a = TestWeatherForecast() # # a.test_getcity() unittest.main()