基于 HttpRunner 的pytest
Mr.Lee 2024-06-03 11:30:00 LinuxPython
基于 Pytest 做接口测试报告
# 1. 安装 HttpRunner
bash -c "$(curl -ksSL https://httprunner.com/script/install.sh)"
1
参考: https://httprunner.com/docs/user-guide/installation/
# 2. 初始化新的Python执行环境
# 创建并是生效环境
❯ python3 -m venv ~/.hrp/venv
❯ source ~/.hrp/venv/bin/activate
❯ cat << EOF > ~/.hrp/venv/pip.conf
[global]
index-url=http://mirrors.aliyun.com/pypi/simple/
[install]
trusted-host=mirrors.aliyun.com
EOF
# 安装依赖
❯ pip install httprunner --upgrade --disable-pip-version-check \
--index-url http://mirrors.aliyun.com/pypi/simple
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# 3. 生成 Pytest
# 3.1 根据har文件生成
hrp convert --from-har tree.har --to-pytest
1
pytest tree_test_test.py
# 3.2 根据 yaml 生成
config:
name: "omm"
base_url: "http://192.168.2.23:58082"
teststeps:
- name: "创建监控对象"
request:
method: POST
url: "/api/v1/indicator/create"
headers:
Accept: "*/*;q=0.5"
Content-Type: application/json
X-Requested-With: XMLHttpRequest
json:
module: data-hamal
service: data-hamal-task
serviceName: ETL任务
cluster: default
region: local
labels:
version: 1.0.0
env: test
host_ip: 10.40.10.2
description: data-hamal-task
exporters:
- instance: '192.168.10.126:9999'
metricPath: /actuator/prometheus
validate:
- check: status_code
assert: equals
expect: 200
msg: assert response status code
- check: headers."Content-Type"
assert: equals
expect: application/json
msg: assert response header Content-Type
- check: body.status
assert: equals
expect: Ax1000700001
msg: assert response body status
- check: body.statusMsg
assert: equals
expect: 正常返回数据
msg: assert response body statusMsg
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
hrun omm.yaml
hrp convert --from-yaml omm.yaml --to-pytest