测试用例关联
hhttprunner中不仅有hook机制,还有用例关联操作,引用内置的RunTestCase类,主要用于在测试步骤中引用其他测试用例
Step(
RunTestCase('request login functions')
.call(TestCaseLogin)
.export(*['token'])
RunTestCase(name):初始化,可自定义的name名称,必填项
.call(类名): 指定引用的测试用例类
.export(变量):指定从引用的测试用例中提取所需的关联变量
# -*-coding:utf-8 -*-
# ** createDate: 2021/3/23 14:22
# ** scriptFile: testcase_student_login_test.py
from httprunner import (HttpRunner, Config, Step, RunRequest)
class TestCaseLogin(HttpRunner):
config = (
Config("login")
.base_url("${ENV(HOSTURL)}")
.verify(False)
)
teststeps = [
Step(
RunRequest('login')
.with_variables(**{'token_length': 32, 'status_code': 201, 'status': 1})
.post('/client/user/auth')
.with_headers(**{"Content-Type": "application/json"})
.with_json({"scenario": "client", "company_id": '${ENV(COMPANYID)}', "user_name": "${ENV(USERNAME)}",
"password": "${ENV(PASSWORD)}", "device_type": "1", "device": ""})
.extract()
.with_jmespath("body.data.token", "token")
.validate()
.assert_equal('status_code', '$status_code', '断言失败')
)
]
if __name__ == '__main__':
TestCaseLogin().test_start()
如果想要下个用例能引用到token值,我们必须要先在当前用例使用 .with_jmespath(token)提取关联值
# -*-coding:utf-8 -*-
# ** createDate: 2021/3/26 17:44
# ** scriptFile: testcase_student_visit_index_test.py
from .testcase_student_login_test import TestCaseLogin
from httprunner import (HttpRunner, Config, Step, RunRequest, RunTestCase)
class TestCaseIndex(HttpRunner):
config = (
Config("index")
.base_url("${ENV(HOSTURL)}")
.verify(False)
)
teststeps = [
Step(
RunTestCase('request login functions')
.call(TestCaseLogin)
.export(*['token'])
),
Step(
RunRequest('index')
.with_variables(**{'status_code': 200, 'status': 1})