L02_HttpRunner的hook(鈎子)機制:(setup/teardown)用例步驟層面的調用
在 HttpRunner 中,hook 機制可以同時在“測試用例層面”和“測試步驟層面”使用,下面看一看同時使用這兩個層面鈎子的情況。
案例二: 用例層面 和 步驟層面 hook 的使用
在 debugtalk.py 檔案中定義的 hook 函數:
def print_msg(type, msg):
print("執行%s:%s" % (type, msg))
引入鈎子 hook 的測試用例檔案:
- 在用例中使用 hook
- 在每個步驟中使用 hook
- config:
name: 測試用例
# 測試用例層面調用鈎子函數
setup_hooks:
- ${print_msg(用例, TestCase-Begin)}
teardown_hooks:
- ${print_msg(用例, TestCase-End)}
- test:
name: 測試步驟 - 打開登入頁面
# 測試步驟層面調用鈎子函數
setup_hooks:
- ${print_msg(步驟, TestStep-Begin)}
teardown_hooks:
- ${print_msg(步驟, TestStep-Begin)}
request:
url: http://localhost/myweb/jxc/index.asp
method: GET
validata:
- eq: [status_code, 200]
- test:
name: 測試步驟 - 進行登入操作
# 測試步驟層面調用鈎子函數
setup_hooks:
- ${print_msg(步驟, TestStep-Begin)}
teardown_hooks:
- ${print_msg(步驟, TestStep-Begin)}
request:
url: http://localhost/myweb/jxc/index.asp?action=login
method: POST
allow_redirects: FALSE
data:
username: admin
pwd: admin
validata:
- eq: [status_code, 302]
執行測試用例,hook 函數被成功調用。
