httprunner2.x--HttpRunner断言:可断言的响应属性

HttpRunner断言:可断言的响应属性

 

通过 response.py 源码文件的错误提示信息中,我们可以看到进行断言时可以涉及到哪些响应的属性。

httprunner-2.5.5\httprunner\response.py

 

 

我们从以下几个角度看看断言编写的例子

  1. headers
  2. elapsed
  3. encoding
  4. reason
  5. ok
  6. text
  7. json
  8. content
  9. url

 

 

断言:headers

   

validate:

- eq: [headers.Content-Length, '1902']

- contains: [headers.Content-Type, text]

  

 

断言:elapsed

响应时间: 小于50000毫秒

validate:
 
- lt: [elapsed.microseconds, 50000]

  

 

elapsed 方法的官方文档地址:http://cn.python-requests.org/zh_CN/latest/api.html#requests.Response

requests.Response

elapsed = None

The amount of time elapsed between sending the request and the arrival of the response (as a timedelta). This property specifically measures the time taken between sending the first byte of the request and finishing parsing the headers. It is therefore unaffected by consuming the response content or the value of the stream keyword argument.

简单翻译:计算的是从发送请求到服务端响应回来这段时间(也就是时间差),发送第一个数据到收到最后一个数据之间,这个时长不受响应的内容影响

 

 

断言:返回的编码类型

注意,写法是 ISO-8859-1,而不是ISO8859-1

 
validate:
 
- eq: [encoding, ISO-8859-1]

  

断言:原因短语

validate:
 
- eq: [reason, OK]
 

  

响应:状态行 (Status-Line)

响应消息的第一行是状态行(stauts-Line),由协议版本以及数字状态码和相关的文本短语组成,各部分间用空格符隔开,除了最后的回车或换行外,中间不允许有回车换行.

Status-Line = HTTP-Version SP Status-Code SP Reason-Phrase CRLF

状态码与原因短语 (Status Code and Reason Phrase)

状态码是试图理解和满足请求的三位数字的整数码。原因短语(Reason-Phrase)是为了给出的关于状态码的文本描述。状态码用于控制条件,而原因短语(Reason-Phrase)是让用户便于阅读。

 

 

断言:布尔类型

ok 断言为True

variables:
 
x : False
 
validate:
 
- eq: [headers.Content-Length, '1902']
 
- eq: [ok, $x]
 

  

 

 

断言:text

按<html>...</html> 判断

 
validate:
 
- contains: [text, </td>]
 

  

 

断言:json

即便返回的不是json数据,按<html>...</html> 判断

validate:
 
- contains: [json, td]

  

 

 

断言:content

按<html>...</html> 判断

validate:
 
- contains: [content, index]
 

  

 

断言:url

validate:
 
- contains: [url, index]

 

失败的断言:headers

headers 是一个 CaseInsensitiveDict类型,不是字符串类型,因此不能用 contains

所以: 断言用 headers.xxxx 的方式

validate:
 
- contains: [headers, GMT]

  

ERROR
 
 
 
validate: headers contains GMT(str) ==> fail
 
 
 
{'Cache-Control': 'no-cache', 'Content-Type': 'text/html', 'Content-Encoding': 'gzip', 'Ex
 
 
 
pires': 'Fri, 13 Mar 2020 13:24:56 GMT', 'Vary': 'Accept-Encoding', 'Server': 'Microsoft-I
 
 
 
IS/7.5', 'Set-Cookie': 'ASPSESSIONIDCCCQBRTB=HALNFFGCIMHLHMHEIKCCPKHC; path=/', 'X-Powered
 
 
 
-By': 'ASP.NET', 'Date': 'Sat, 14 Mar 2020 13:24:57 GMT', 'Content-Length': '1902'}
 
 
 
(CaseInsensitiveDict) contains GMT(str)

  

 

 

 

 

 

 

 

posted @ 2021-06-04 10:28  莫使娇躯空对月  阅读(459)  评论(0编辑  收藏  举报