测试工具httprunner试用

IT知识
190
0
0
2024-01-19
标签   测试工具

https://github.com/httprunner/httprunner是一个API 测试工具,支持 HTTP(S) / HTTP2 / WebSocket / RPC 等网络协议,涵盖接口测试、性能测试、数字体验监测等测试类型。简单易用,功能强大,具有丰富的插件化机制和高度的可扩展能力。

下面体验下如何使用,首先安装

% bash -c "$(curl -ksSL https://httprunner.com/script/install.sh)"
✔ Detect target hrp package...
Latest version: v4.3.1
Current OS: darwin
Current ARCH: amd64
Download package: hrp-v4.3.1-darwin-amd64.tar.gz
! Invalid download url: https://httprunner.oss-cn-beijing.aliyuncs.com/hrp-v4.3.1-darwin-amd64.tar.gz
Download url: https://github.com/httprunner/httprunner/releases/download/v4.3.1/hrp-v4.3.1-darwin-amd64.tar.gz

✔ Downloading...
$ curl -kL https://github.com/httprunner/httprunner/releases/download/v4.3.1/hrp-v4.3.1-darwin-amd64.tar.gz -o hrp-v4.3.1-darwin-amd64.tar.gz
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
100 7579k  100 7579k    0     0  28629      0  0:04:31  0:04:31 --:--:-- 39030

✔ Created temp dir...
$ mktemp -d -t hrp.XXXX
/var/folders/2n/42n_1dfd6kjd6s3k7bt4cb3h0000gn/T/hrp.XXXX.ljrysUnc
$ mv hrp-v4.3.1-darwin-amd64.tar.gz /var/folders/2n/42n_1dfd6kjd6s3k7bt4cb3h0000gn/T/hrp.XXXX.ljrysUnc && cd /var/folders/2n/42n_1dfd6kjd6s3k7bt4cb3h0000gn/T/hrp.XXXX.ljrysUnc

✔ Extracting...
$ tar -xzf hrp-v4.3.1-darwin-amd64.tar.gz
$ ls -lh
total 58432
-rw-r--r--  1 xiazemin  staff    23K Dec 26 23:46 CHANGELOG.md
-rw-r--r--  1 xiazemin  staff    11K Dec 26 23:46 LICENSE
-rw-r--r--  1 xiazemin  staff   9.5K Dec 26 23:46 README.md
-rwxr-xr-x  1 xiazemin  staff    20M Dec 26 23:46 hrp
-rw-r--r--  1 xiazemin  staff   7.4M Apr 15 20:06 hrp-v4.3.1-darwin-amd64.tar.gz

✔ Installing...
bash: line 119: hrp: command not found
$ chmod +x hrp && mv hrp /usr/local/bin/

✔ Check installation...
$ command -v hrp
/usr/local/bin/hrp
$ hrp -v
{"level":"info","path":"/Users/xiazemin/.hrp/x509","time":"2023-04-15T20:06:24+08:00","message":"create folder"}
hrp version v4.3.1
$ hrp -h

██╗  ██╗████████╗████████╗██████╗ ██████╗ ██╗   ██╗███╗   ██╗███╗   ██╗███████╗██████╗
██║  ██║╚══██╔══╝╚══██╔══╝██╔══██╗██╔══██╗██║   ██║████╗  ██║████╗  ██║██╔════╝██╔══██╗
███████║   ██║      ██║   ██████╔╝██████╔╝██║   ██║██╔██╗ ██║██╔██╗ ██║█████╗  ██████╔╝
██╔══██║   ██║      ██║   ██╔═══╝ ██╔══██╗██║   ██║██║╚██╗██║██║╚██╗██║██╔══╝  ██╔══██╗
██║  ██║   ██║      ██║   ██║     ██║  ██║╚██████╔╝██║ ╚████║██║ ╚████║███████╗██║  ██║
╚═╝  ╚═╝   ╚═╝      ╚═╝   ╚═╝     ╚═╝  ╚═╝ ╚═════╝ ╚═╝  ╚═══╝╚═╝  ╚═══╝╚══════╝╚═╝  ╚═╝

HttpRunner is an open source API testing tool that supports HTTP(S)/HTTP2/WebSocket/RPC
network protocols, covering API testing, performance testing and digital experience
monitoring (DEM) test types. Enjoy! ✨ 🚀 ✨

License: Apache-2.0
Website: https://httprunner.com
Github: https://github.com/httprunner/httprunner
Copyright 2017 debugtalk

Usage:
  hrp [command]

Available Commands:
  adb          simple utils for android device management
  boom         run load test with boomer
  build        build plugin for testing
  completion   Generate the autocompletion script for the specified shell
  convert      convert to JSON/YAML/gotest/pytest testcases
  curl         run integrated curl command
  dns          DNS resolution for different source and record types
  help         Help about any command
  ios          simple utils for ios device management
  ping         run integrated ping command
  pytest       run API test with pytest
  run          run API test with go engine
  startproject create a scaffold project
  traceroute   run integrated traceroute command
  wiki         visit https://httprunner.com

Flags:
  -h, --help               help for hrp
      --log-json           set log to json format
  -l, --log-level string   set log level (default "INFO")
      --venv string        specify python3 venv path
  -v, --version            version for hrp

Use "hrp [command] --help" for more information about a command.

安装完毕后初始化项目

% hrp startproject demo
8:34PM INF Set log to color console
8:34PM ??? Set log level
8:34PM INF create new scaffold project force=false pluginType=py projectName=demo
8:34PM INF create folder path=demo
8:34PM INF create folder path=demo/har
8:34PM INF create file path=demo/har/.keep
8:34PM INF create folder path=demo/testcases
8:34PM INF create folder path=demo/results
8:34PM INF create file path=demo/results/.keep
8:34PM INF dump data to json path=/learn/mock/httprunner/demo/proj.json
8:34PM INF create file path=demo/.gitignore
8:34PM INF create file path=demo/.env
8:34PM INF create file path=demo/testcases/demo.json
8:34PM INF create file path=demo/testcases/requests.json
8:34PM INF create file path=demo/testcases/requests.yml
8:34PM INF create file path=demo/testcases/ref_testcase.yml
8:34PM INF start to create hashicorp python plugin
8:34PM INF create file path=demo/debugtalk.py
8:34PM INF ensure python3 venv packages=["funppy==v0.5.0","httprunner==v4.3.0"] python3=/Users/xiazemin/.hrp/venv/bin/python3
8:34PM INF python package is ready name=funppy version=v0.5.0
8:34PM INF python package is ready name=httprunner version=v4.3.0
8:34PM INF set python3 executable path Python3Executable=/Users/xiazemin/.hrp/venv/bin/python3
8:34PM INF create scaffold success projectName=demo

看下生成的目录

% tree
.
|____proj.json
|____results
| |____.keep
|____.gitignore
|____.env
|____debugtalk.py
|____har
| |____.keep
|____testcases
| |____demo.json
| |____ref_testcase.yml
| |____requests.json
| |____requests.yml

用charles抓包导出点击右键,在悬浮的菜单目录中点击【Export...】,格式选择HTTP Archive(.har)后保存即可;假设我们保存的文件名称为test.har

转化成case

% hrp convert ./har/baidu.har -d ./testcases
9:02PM INF Set log to color console
9:02PM ??? Set log level
9:02PM INF load file path=./har/baidu.har
9:02PM INF convert teststep method=GET url=https://api.vc.bilibili.com/session_svr/v1/session_svr/single_unread?build=0&mobi_app=web&unread_type=0
9:02PM INF convert teststep method=GET url=https://www.baidu.com/sugrec?pre=1&p=3&ie=utf-8&json=1&prod=pc&from=pc_web&sugsid=38515,36555,38470,38467,36802,37935,38505,26350&wd=convert%20input%20sample%20failed%20error%3D%22load%20har%20file%20failed%3A%20re&req=2&bs=8%3A56PM%20WRN%20convert%20input%20sample%20failed%20error%3D%22load%20har%20file%20failed%3A%20re&pbs=8%3A56PM%20WRN%20convert%20input%20sample%20failed%20error%3D%22load%20har%20file%20failed%3A%20re&csor=0&pwd=8convert%20input%20sample%20failed%20error%3D%22load%20har%20file%20failed%3A%20re&cb=jQuery110202954672835349119_1681563304796&_=1681563304812
9:02PM INF convert teststep method=GET url=https://www.baidu.com/sugrec?pre=1&p=3&ie=utf-8&json=1&prod=pc&from=pc_web&sugsid=38515,36555,38470,38467,36802,37935,38505,26350&wd=convert%20input%20sample%20failed%20error%3D%22load%20har%20file%20failed%3A%20re&req=2&bs=8%3A56PM%20WRN%20convert%20input%20sample%20failed%20error%3D%22load%20har%20file%20failed%3A%20re&pbs=8%3A56PM%20WRN%20convert%20input%20sample%20failed%20error%3D%22load%20har%20file%20failed%3A%20re&csor=0&pwd=convert%20input%20sample%20failed%20error%3D%22load%20har%20file%20failed%3A%20re&cb=jQuery110202954672835349119_1681563304796&_=1681563304813
9:02PM INF convert teststep method=GET url=https://www.baidu.com/sugrec?pre=1&p=3&ie=utf-8&json=1&prod=pc&from=pc_web&sugsid=38515,36555,38470,38467,36802,37935,38505,26350&wd=%20error%3D%22load%20har%20file%20failed%3A%20re&req=2&bs=8%3A56PM%20WRN%20convert%20input%20sample%20failed%20error%3D%22load%20har%20file%20failed%3A%20re&pbs=8%3A56PM%20WRN%20convert%20input%20sample%20failed%20error%3D%22load%20har%20file%20failed%3A%20re&csor=0&pwd=convert%20input%20sample%20failed%20error%3D%22load%20har%20file%20failed%3A%20re&cb=jQuery110202954672835349119_1681563304796&_=1681563304814
9:02PM INF convert teststep method=GET url=https://api.vc.bilibili.com/session_svr/v1/session_svr/single_unread?build=0&mobi_app=web&unread_type=0
9:02PM INF convert teststep method=GET url=https://api.vc.bilibili.com/session_svr/v1/session_svr/single_unread?build=0&mobi_app=web&unread_type=0
9:02PM INF dump data to json path=/Users/xiazemin/bilibili/live/learn/mock/httprunner/demo/testcases/baidu_test.json
9:02PM INF conversion completed output files=["testcases/baidu_test.json"]

运行case并输出测试报告

% hrp run ./testcases/baidu_test.json --gen-html-report

9:08PM INF validate body.msg assertMethod=equals checkExpr=body.msg checkValue=0 checkValueType=string expectValue=0 expectValueType=string result=true
9:08PM INF validate body.ttl assertMethod=equals checkExpr=body.ttl checkValue=1 checkValueType=int64 expectValue=1 expectValueType=int64 result=true
9:08PM INF run step end exportVars=null step="GET https://api.vc.bilibili.com/session_svr/v1/session_svr/single_unread" success=true type=request
9:08PM INF run testcase end testcase="testcase description"
9:08PM INF create folder path=results/20230415210822
9:08PM INF generate HTML report path=results/20230415210822/report.html
9:08PM INF quit hashicorp plugin process
2023-04-15T21:08:25.843+0800 [WARN]  grpc-py: plugin failed to exit gracefully
2023-04-15T21:08:25.847+0800 [ERROR] grpc-py: plugin process exited: path=/Users/xiazemin/.hrp/venv/bin/python3 pid=53347 error="signal: killed"