测试工具:hurl

文章目录

  • Hurl
    • install
    • start
      • demo
    • 功能
      • 变量
      • Request
      • Response
        • Capturing values 捕获值
        • Asserts 断言
          • Implicit asserts 隐式断言
          • Explicit asserts 显式断言
        • Filerts 过滤器
      • CI/CD

Hurl

官网:https://hurl.dev/

Hurl 是一个命令行工具,它运行以简单的纯文本格式定义的 HTTP 请求。
它可以发送请求、捕获值并评估对标头和正文响应的查询

install

  • dowload
    https://github.com/Orange-OpenSource/hurl/releases
  • run
hurl --version

start

  1. 编写.hurl文件
  2. Running Tests
hurl [options] [FILE...]

demo

  1. 创建 basic.hurl文件
GET http://localhost:3000
  1. 执行 basic.hurl测试
hurl basic.hurl
# test mode
hurl --test basic.hurl

功能

变量

传入变量,在hurl文件中用{{ }} 使用变量

  1. --variable 选项
hurl --variable host=127.0.0.1 --variable id=1234 test.hurl
  1. --variables-file 选项
  • 编写 vars.env文件
host=127.0.0.1
id=1234
hurl --variables-file vars.env test.hurl
  1. HURL_name=value 环境变量
export HURL_host=example.net
export HURL_id=1234 
hurl test.hurl
  1. hurl文件中:[Options]
GET https://{{host}}/{{id}}/status
[Options]
variable: host=example.net
variable: id=1234
HTTP 304

GET https://{{host}}/health
HTTP 200

Request

  • 基本格式
PUT https://sample.net
accept: */*
x-powered-by: Express
user-agent: Test
[QueryStringParams]
...查询字符串参数
[FormParams]
...表单参数
[BasicAuth]
...基本认证
[Cookies]
...Cookies
...
...
{
  "type": "FOO",
  "value": 356789,
  "ordered": true,
  "index": 10
}
  • cookie
    同一 Hurl 文件中的请求共享 cookie 存储

Response

Capturing values 捕获值

从 HTTP 响应中提取可选值,并存储在命名变量

  • 语法
[Captures]
my_var: QueryType query
  • QueryType支持:
    status
    header
    url
    cookie
    body
    bytes
    xpath
    jsonpath
    regex
    variable
    duration
    certificate
  • demo
[Captures]
# 状态
my_status: status
# 标头
next_url: header "Location"
# url
landing_url: url
# cookie
session-id: cookie "LSID"
# body
my_body: body
# JSONPath
contact-id: jsonpath "$['id']"

Asserts 断言

允许在每个响应中插入断言

[Asserts]
Implicit asserts 隐式断言
  • Version - Status
    HTTP/1.0, HTTP/1.1, HTTP/2 or HTTP status code
  • Headers 标头
    name:value
GET https://example.org/404.html
HTTP 404
HTTP/1.1 404
HTTP/2 200

Location: https://example.org/home
Set-Cookie: theme=light
Set-Cookie: sessionToken=abc123; Expires=Wed, 09 Jun 2021 10:18:14 GMT
Explicit asserts 显式断言
  • 结构
    query predicateType predicateValue

  • query
    格式与Capturing相同

  • Predicates

==	
!=	
>
<
>=	
<=	
startsWith
endsWith
contains	  
includes	  
matches	    
exists	    
isBoolean	  
isCollection
isDate	    
isEmpty	    
isFloat	    
isInteger	  
isString	  
  • demo
[Asserts]
status < 300

header "Location" contains "www.example.net"
header "Last-Modified" matches /\d{2} [a-z-A-Z]{3} \d{4}/

url == "https://example.org/redirected"

cookie "LSID" == "DQAAAKEaem_vYg"
cookie "LSID[Value]" == "DQAAAKEaem_vYg"
cookie "LSID[Expires]" exists
cookie "LSID[Expires]" contains "Wed, 13 Jan 2021"
cookie "LSID[Max-Age]" not exists
cookie "LSID[Domain]" not exists
cookie "LSID[Path]" == "/accounts"
cookie "LSID[Secure]" exists
cookie "LSID[HttpOnly]" exists
cookie "LSID[SameSite]" equals "Lax"

header "Content-Type" == "text/html; charset=gb2312"
bytes contains hex,c4e3bac3cac0bde7; # 你好世界 encoded in GB 2312
body contains "你好世界"

jsonpath "$.slideshow.author" == "Yours Truly"
jsonpath "$.slideshow.slides[0].title" contains "Wonder"
jsonpath "$.slideshow.slides" count == 2
jsonpath "$.slideshow.date" != null
jsonpath "$.slideshow.slides[*].title" includes "Mind Blowing!"

variable "pets" count == 200

duration < 1000   # Check that response time is less than one second
Filerts 过滤器

用于转换查询提取的值

count
daysAfterNow
daysBeforeNow
decode
format
htmlEscape
replace
split

CI/CD

  1. 集成脚本
    把hurl文件放在目录下
    编写sh脚本运行目录下的测试用例

  2. 集成到 git commit
    GitHub Action, GitLab CI/CD

  3. 生成报告

你可能感兴趣的:(测试,postman,测试用例)