httprunner之业务解耦

该博客展示了使用HTTPRunner进行接口自动化测试的实践。首先,通过登录接口获取token,然后在后续的请求中引用该token进行身份验证。测试用例包括登录接口的调用,验证响应状态码、内容类型和返回消息,以及删除指定话术的接口,同样检查响应状态码和消息。整个过程体现了HTTPRunner在接口测试中的应用。
摘要由CSDN通过智能技术生成

A文件(登录取token被引用文件):

# NOTE: Generated By HttpRunner v3.1.8
# FROM: har\.\login.har
import allure

from httprunner import HttpRunner, Config, Step, RunRequest, RunTestCase


class TestCaseLogin(HttpRunner):
    config = Config("testcase description").verify(False)

    teststeps = [

        Step(RunRequest("/adminapi/login").post("http://不方便展示/adminapi/login").with_headers(
            **{'Host': '不方便展示', 'Content-Length': '49', 'Accept': 'application/json, text/plain, */*',
               'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.74 Safari/537.36 Edg/99.0.1150.55',
               'Content-Type': 'application/json;charset=UTF-8', 'Origin': 'http://不方便展示',
               'Referer': 'http://不方便展示/admin/login', 'Accept-Encoding': 'gzip, deflate',
               'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6',
               'Cookie': 'WS_ADMIN_URL=ws://不方便展示/notice; WS_CHAT_URL=ws://不方便展示/msg; collapsed=false; think_lang=zh-cn; PHPSESSID=ec012172dbb8925955ddf54ba37a38d5',
               'Connection': 'keep-alive'}).with_cookies(
            **{'WS_ADMIN_URL': 'ws://不方便展示/notice', 'WS_CHAT_URL': 'ws://不方便展示/msg',
               'collapsed': 'false', 'think_lang': 'zh-cn', 'PHPSESSID': 'ec012172dbb8925955ddf54ba37a38d5'}).with_json(
            {'account': '***', 'pwd': '***', 'imgcode': '22'})
            .extract().with_jmespath("body.data.token","token").validate().assert_equal("status_code",
                                                                                            200).assert_equal(
            'headers."Content-Type"', "application/json; charset=utf-8").assert_equal("body.status", 200).assert_equal(
            "body.msg", "ok")),

    ]


if __name__ == "__main__":
    TestCaseLogin().test_start()

如果想练习去gitee找个开源

B文件:

# NOTE: Generated By HttpRunner v3.1.8
# FROM: har\.\test_hs.har
import allure

from httprunner import HttpRunner, Config, Step, RunRequest, RunTestCase
from homework.httprunner.test_crmeb.testcases.login_test import TestCaseLogin as login


class TestCaseTestHs(HttpRunner):
    config = Config("testcase description").verify(False)

    teststeps = [

        Step(
            RunTestCase("login")
                .call(login)
                .export("token") #todo:此处引用
        ),

        Step(
            RunRequest("/adminapi/app/wechat/speechcraft").post(
                "http://null/adminapi/app/wechat/speechcraft").with_headers(
                **{'Host': 'null', 'Content-Length': '64',
                   'Accept': 'application/json, text/plain, */*',
                   'Authori-zation': 'Bearer ' + '$token',
                   'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.74 Safari/537.36 Edg/99.0.1150.55',
                   'Content-Type': 'application/json;charset=UTF-8', 'Origin': 'http://null',
                   'Referer': 'http://null/admin/setting/store_service/speechcraft',
                   'Accept-Encoding': 'gzip, deflate',
                   'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6',
                   'Cookie': 'WS_ADMIN_URL=ws://null/notice; WS_CHAT_URL=ws://null/msg; collapsed=false; think_lang=zh-cn; PHPSESSID=ec012172dbb8925955ddf54ba37a38d5; uuid=1; token=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiIxMzkuMTU5LjIzMy4xMjk6ODg5OSIsImF1ZCI6IjEzOS4xNTkuMjMzLjEyOTo4ODk5IiwiaWF0IjoxNjQ4ODA2MDM2LCJuYmYiOjE2NDg4MDYwMzYsImV4cCI6MTY1MTM5ODAzNiwianRpIjp7ImlkIjoxLCJ0eXBlIjoiYWRtaW4ifX0.Y7Z0vWbzo8JWLy-Ysolyj20HFmyqsVWlXLmEdRld0YI; expires_time=1651398036',
                   'Connection': 'keep-alive'}).with_cookies(
                **{'WS_ADMIN_URL': 'ws://null/notice', 'WS_CHAT_URL': 'ws://null/msg',
                   'collapsed': 'false', 'think_lang': 'zh-cn', 'PHPSESSID': 'ec012172dbb8925955ddf54ba37a38d5',
                   'uuid': '1',
                   'token': '$token',
                   'expires_time': '1651398036'}).with_json(
                {'cate_id': '', 'title': "${cid(b)}", 'message': 'test33', 'sort': 0}).validate().assert_equal(
                "status_code", 200).assert_equal('headers."Content-Type"',
                                                 "application/json; charset=utf-8").assert_equal(
                "body.status", 200).assert_equal("body.msg", "创建话术成功")),

        Step(RunRequest("/adminapi/app/wechat/speechcraft/"+"${cid(a)}").delete(
            "http://null/adminapi/app/wechat/speechcraft/"+"${cid(a)}").with_headers(
            **{'Host': 'null', 'Content-Length': '0', 'Accept': 'application/json, text/plain, */*',
               'Authori-zation': 'Bearer '+'$token',
               'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.74 Safari/537.36 Edg/99.0.1150.55',
               'Content-Type': 'text/plain;charset=UTF-8', 'Origin': 'http://null',
               'Referer': 'http://null/admin/setting/store_service/speechcraft',
               'Accept-Encoding': 'gzip, deflate', 'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6',
               'Cookie': 'WS_ADMIN_URL=ws://null/notice; WS_CHAT_URL=ws://null/msg; collapsed=false; think_lang=zh-cn; PHPSESSID=ec012172dbb8925955ddf54ba37a38d5; uuid=1; token=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiIxMzkuMTU5LjIzMy4xMjk6ODg5OSIsImF1ZCI6IjEzOS4xNTkuMjMzLjEyOTo4ODk5IiwiaWF0IjoxNjQ4ODA2MDM2LCJuYmYiOjE2NDg4MDYwMzYsImV4cCI6MTY1MTM5ODAzNiwianRpIjp7ImlkIjoxLCJ0eXBlIjoiYWRtaW4ifX0.Y7Z0vWbzo8JWLy-Ysolyj20HFmyqsVWlXLmEdRld0YI; expires_time=1651398036',
               'Connection': 'keep-alive'}).with_cookies(
            **{'WS_ADMIN_URL': 'ws://null/notice', 'WS_CHAT_URL': 'ws://null/msg',
               'collapsed': 'false', 'think_lang': 'zh-cn', 'PHPSESSID': 'ec012172dbb8925955ddf54ba37a38d5',
               'uuid': '1',
               'token': '$token',
               'expires_time': '1651398036'}).validate().assert_equal("status_code", 200).assert_equal(
            'headers."Content-Type"', "application/json; charset=utf-8").assert_equal("body.status", 200).assert_equal(
            "body.msg", "删除成功")),

    ]


if __name__ == "__main__":
    TestCaseTestHs().test_start()

B文件有debugtalk方法,不要过分解读

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值