我們正在推動功能測試進行自動化用例的編寫,是以我們沿用httprunner中yaml格式進行用例編寫。
在用例編寫中,我們遇到了需要設定前置條件的情況,而2.x的實作方式已經不能沿用到3.x裡面,yml轉化為.py時直接給忽略了,譬如這樣的代碼:
config:
name: testcase description
base_url: "http://192.152.104.111:8080"
variables:
username: "admin"
password: "[email protected]"
export: ["token"]
setup_hooks:
- ${hook_up()}
teardown_hooks:
- ${hook_down()}
轉化成的.py檔案中沒有對應的代碼内容:
class TestCaseLogin(HttpRunner):
config = (
Config("testcase description")
.variables(**{"username": "admin", "password": "[email protected]"})
.base_url("http://192.152.104.111:8080")
.export(*["token"])
)
teststeps = [
Step(
RunRequest("public-key")
.get("/secret/public-key")
研究了很久也沒有搞出針對用例進行setup,teardown的實作方式。但是通過對測試步驟進行setup的操作時OK的,是以我就想到了曲線救國的思路。
在測試步驟第一步設定setup,在測試步驟最後一步設定teardown,也能達到用例級别的hook機制
yml代碼如下:
config:
name: "batch user order sync interface"
base_url: "http://192.152.113.23:8080"
verify: False
variables:
phone: "13500001|13500002"
localpath: "D:\\xx.txt"
remotepath: "/cmiot/ccmp/srv_file/xx.txt"
teststeps:
-
name: sync interface
setup_hooks:
- ${update_file_content($phone,$localpath)}
- ${load_file_to_service($localpath,$remotepath)}
teardown_hooks:
- ${hook_down()}
request:
method: POST
url: /ccmp/interface
headers:
Content-Type: "txt/xml"
轉換後的python代碼如下:
from httprunner import HttpRunner, Config, Step, RunRequest, RunTestCase
class TestCaseCrmBatchchangeofferingbyfile(HttpRunner):
config = (
Config("batch user order sync interface")
.variables(
**{
"phone": "13500001|13500002",
"localpath": "D:\\xx.txt",
"remotepath": "/cmiot/ccmp/srv_file/xx.txt",
}
)
.base_url("http://192.152.113.23:8080")
.verify(False)
)
teststeps = [
Step(
RunRequest("sync interface")
.setup_hook("${update_file_content($phone,$localpath)}")
.setup_hook("${load_file_to_service($localpath,$remotepath)}")
.post("/ccmp/interface")
.with_headers(**{"Content-Type": "txt/xml"})
說明格式OK,能夠正常轉換。
并且在setup_hook下可以設定多個方法調用,親測有效。暫時分享到這裡,後面如果能實作用例級别的hook機制,也會進行文章内容更新。