httprunner学习-hook 机制实现setup和teardown
前言
unittest框架里面有个非常好的概念:前置( setUp
)和后置( tearDown
)处理器,真正会用的人不多。
HttpRunner 实际上也是从用的unittest框架,里面也有前置 setup_hooks
和后置 teardown_hooks
的概念。
- setup_hooks: 在整个用例开始执行前触发 hook 函数,主要用于准备工作。
- teardown_hooks: 在整个用例结束执行后触发 hook 函数,主要用于测试后的清理工作。
config添加hook机制
在config里添加 setup_hooks
和 teardown_hooks
实际上相当于unittest里面的 setUpClass
和 tearDownClass
的概念。
它的作用是在整个 YAML/JSON
文件的用例只执行一次。接下来看下案例的执行情况就知道了,在debugtalk.py写2个简单函数
# debugtalk.py
# 上海悠悠,QQ交流群:750815713
def hook_up():
print("前置操作:setup!")
def hook_down():
print("后置操作:teardown!")
在 test_hook_demo.yml
文件写以下2个test用例
# 上海悠悠,QQ交流群:750815713
- config:
name: test_demo
variables: {}
setup_hooks:
- ${hook_up()}
teardown_hooks:
- ${hook_down()}
- test:
name: test_demo case1
request:
url: http://127.0.0.1:8000/api/test/demo
method: GET
headers:
Content-Type: application/json
User-Agent: python-requests/2.18.4
json:
username: test
password: 123456
extract:
- mail: content.datas.0.mail # 提取mail
validate:
- eq: [status_code, 200]
- equals: [content.code, 0]
- equals: [content.msg, success!]
- test:
name: test_demo case2
request:
url: http://127.0.0.1:8000/api/test/demo
method: GET
headers:
Content-Type: application/json
User-Agent: python-requests/2.18.4
json:
username: test
password: 123456
extract:
- mail: content.datas.0.mail # 提取mail
validate:
- eq: [status_code, 200]
执行用例
D:\soft\untitled\projectdemo>hrun test_hook_demo.yml
前置操作:setup!
test_demo case1
INFO GET http://127.0.0.1:8000/api/test/demo
INFO status_code: 200, response_time(ms): 0.0 ms, response_length: 255 bytes
INFO start to extract from response object.
INFO start to validate.
.
test_demo case2
INFO GET http://127.0.0.1:8000/api/test/demo
INFO status_code: 200, response_time(ms): 15.66 ms, response_length: 255 bytes
INFO start to extract from response object.
INFO start to validate.
.
----------------------------------------------------------------------
Ran 2 tests in 0.031s
OK
后置操作:teardown!
INFO Start to render Html report ...
INFO Generated Html report: D:\soft\untitled\projectdemo\reports\1569512002.html
执行后会发现config里面的 setup_hooks 作用域是整个脚本文件,并且在测试开始前只执行一次,一般用于测试的数据准备工作。
teardown_hooks 在测试用例结束后只执行一次,一般用于数据清理。
test用例添加hook机制
在test测试用例里面也可以添加 setup_hooks
和 teardown_hooks
,它的作用域是当前test用例有效,以下是用过简单的输出log的函数,主要看下运行的顺序
# debugtalk.py
def hook_log(var=''):
print("用例执行log:%s" % var)
在test_hook_demo2.yml 文件写以下2个test用例
# 上海悠悠,QQ交流群:750815713
- config:
name: test_demo
variables: {}
setup_hooks:
- ${hook_up()}
teardown_hooks:
- ${hook_down()}
- test:
name: test_demo case1
request:
url: http://127.0.0.1:8000/api/test/demo
method: GET
headers:
Content-Type: application/json
User-Agent: python-requests/2.18.4
json:
username: test
password: 123456
extract:
- mail: content.datas.0.mail # 提取mail
validate:
- eq: [status_code, 200]
- equals: [content.code, 0]
- equals: [content.msg, success!]
setup_hooks:
- ${hook_log(--------测试用例 1 开始前的准备-------)}
teardown_hooks:
- ${hook_log(-------测试用例 1 结束--------)}
- test:
name: test_demo case2
request:
url: http://127.0.0.1:8000/api/test/demo
method: GET
headers:
Content-Type: application/json
User-Agent: python-requests/2.18.4
json:
username: test
password: 123456
extract:
- mail: content.datas.0.mail # 提取mail
validate:
- eq: [status_code, 200]
setup_hooks:
- ${hook_log(---------测试用例 2 开始前的准备---------)}
teardown_hooks:
- ${hook_log(----------测试用例 2 结束---------)}
运行结果
D:\soft\untitled\projectdemo>hrun test_hook_demo2.yml
前置操作:setup!
test_demo case1
用例执行log:--------测试用例 1 开始前的准备-------
INFO GET http://127.0.0.1:8000/api/test/demo
INFO status_code: 200, response_time(ms): 15.62 ms, response_length: 255 bytes
用例执行log:-------测试用例 1 结束--------
INFO start to extract from response object.
INFO start to validate.
.
test_demo case2
用例执行log:---------测试用例 2 开始前的准备---------
INFO GET http://127.0.0.1:8000/api/test/demo
INFO status_code: 200, response_time(ms): 0.0 ms, response_length: 255 bytes
用例执行log:----------测试用例 2 结束---------
INFO start to extract from response object.
INFO start to validate.
.
----------------------------------------------------------------------
Ran 2 tests in 0.016s
OK
后置操作:teardown!
INFO Start to render Html report ...
INFO Generated Html report: D:\soft\untitled\projectdemo\reports\1569512879.html
运行结果可以看出,在test用例里面的前置和后置操作是针对单个用例的
转:https://www.cnblogs.com/yoyoketang/p/11595125.html