1. 通用关键字
可参考官方文档:
Write Testcase - HttpRunner V3.x Docs
2. 特别关键字
2.1. 步骤step前置
2.1.1. setup_hook
关键源码
def setup_hook(self, hook: Text, assign_var_name: Text = None) -> "RunRequest":
if assign_var_name:
self.__step.setup_hooks.append({assign_var_name: hook})
else:
self.__step.setup_hooks.append(hook)
return self
2.2. 步骤step后置
2.2.1. teardown_hook
关键源码
def teardown_hook(
self, hook: Text, assign_var_name: Text = None
) -> "RequestWithOptionalArgs":
if assign_var_name:
self.__step.teardown_hooks.append({assign_var_name: hook})
else:
self.__step.teardown_hooks.append(hook)
return self
2.3. 重试
2.3.1. with_retry
关键源码
def with_retry(self, retry_times, retry_interval) -> "RunRequest":
self.__step.retry_times = retry_times
self.__step.retry_interval = retry_interval
return self
2.4.
3. demo
示例:
3.1. .setup_hook('${setup_hooks_request($request)}')
3.2. .teardown_hook('${teardown_assert_response($response)}')
teststeps = [
Step(
RunRequest('login')
.with_variables(**{'token_length': 32, 'status_code': 201, 'status': 1})
.setup_hook('${setup_hooks_request($request)}')
.post('/client/user/auth')
.with_headers(**{"Content-Type": "application/json"})
.with_json({"scenario": "client", "company_id": '$company_id',
"user_name": "$username", "password": "$password",
"device_type": "$device_type", "device": ""})
.teardown_hook('${teardown_assert_response($response)}')
.extract()
.with_jmespath("body.data.token", "token")
.validate()
.assert_equal('status_code', '$status_code', '断言失败')
.assert_equal('body.status', '$status', '断言失败')
.assert_length_equal('body.data.token', '$token_length', '断言失败')
)
]