def jsan(self): """ :return: response jsane traversable object """ try: return jsane.from_dict(self.response.json()) except json.JSONDecodeError as e: logger.debug('<= err msg => \n{}'.format(e)) logger.debug('<= response content => \n{}'.format(self.response.content)) pytest.fail('response data format does not match the JSON format')
def send(self, method, url, variable=None, para=None, json=None, data=None, header=None, files=None): """ send request. Params: | method | request method | # POST | | url | target url | | variable | kwargs in url | | params | kwargs for get method | | data | kwargs in request body form format| | headers | custom header | | files | # TBD | :return: response """ if variable: url = url.format(**variable) req = requests.Request(method, url, params=para, json=json, data=data, headers=header, files=files) prepped = self.session.prepare_request(req) self.response = self.session.send(prepped) logger.debug('<= request url => \n{}'.format(self.response.request.url)) body = json or data logger.debug('<= request data => \n{}'.format(jsane.dumps(body, indent=4, ensure_ascii=False))) logger.debug('<= response data => \n{}'.format(jsane.dumps(self.jsan().r(), indent=4, ensure_ascii=False))) logger.debug('<= time elapsed => \n{}s'.format(self.response.elapsed.total_seconds())) return self.response
def validate_resp(self, scm): """ :param scm: schema data :return: """ data = self.response.json() scm = self._update_params(scm) schema = Schema(scm, ignore_extra_keys=True) try: schema.validate(data) logger.debug('<= schema template => \n{}'.format(scm)) except SchemaError as e: logger.error('<= schema template => \n{}'.format(scm)) logger.error('<= err msg => \n{}'.format(e)) pytest.fail(msg=str(e), pytrace=False)
def validate_data(self, json_query: str, scm: str): """ 校验返回体的部分字段 Args: json_query (str): 待校验数据的json_query, scm (str): 数据校验模板 """ data = jmespath.search(json_query, self.response.json()) scm = self._update_params(scm) schema = Schema(scm, ignore_extra_keys=True) try: schema.validate(data) logger.debug('<= schema template => \n{}'.format(scm)) except SchemaError as e: logger.error('<= schema template => \n{}'.format(scm)) logger.error('<= err msg => \n{}'.format(e)) pytest.fail(msg=str(e), pytrace=False)