httprunner3.x 测试用例关联及运行

测试用例关联

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})
           
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值