Foreword
unittest framework, which has a very good concept: front ( setUp
) and post ( tearDonw
) processor, not many people will actually use.
HttpRunner actually used frame from unittest, which also has front setup_hooks
and rear teardown_hooks
concept.
- setup_hooks: Trigger hook function before the entire use case started, mainly for preparatory work.
- teardown_hooks: After the implementation of the entire case ends with trigger hook functions, mainly for cleanup after the test.
config add hook mechanism
Add in the config inside setup_hooks
and teardown_hooks
actually equivalent unittest inside setUpClass
and tearDownClass
concepts.
Its role is in the whole YAML/JSON
execution only once use case file. Then look at the implementation of the case to know, write two simple functions in debugtalk.py
# debugtalk.py
def hook_up():
print("前置操作:setup!")
def hook_down():
print("后置操作:teardown!")
In the test_hook_demo.yml
file write the following two test use cases
- 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]
Example execution
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
After the execution will find the config inside setup_hooks scope is the entire script file, and perform only once before the start of the test, generally used for data preparation for the test.
teardown_hooks executed only once after the test, generally used for data cleansing.
Add test cases with hook mechanism
Test cases may be added inside the test setup_hooks
and teardown_hooks
it is the current test scope effective use cases, the following is used a simple function of the output of the log, the sequence of operations mainly facie
# debugtalk.py
def hook_log(var=''):
print("用例执行log:%s" % var)
The following two test write use cases in test_hook_demo2.yml file
- 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 结束---------)}
operation result
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
Run results can be seen in the embodiment with the test inside the pre- and post-operation is for a single use case