ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

HttpRunner3.X - 项目实例一

2021-07-17 15:32:29  阅读:258  来源: 互联网

标签:body __ 项目 json equal assert application HttpRunner3 实例


一、前言

  前面讲的比较理论,本篇主要用实际项目,体现下HttpRunner的一些基本用法。

二、项目场景实例说明

1、业务流程:登录——创建版单——领取版单

2、接口信息如下:

  • 登录:/auth/login_by_password
  • 创建版单:type/add
  • 领取版单:type/received

3、接口依赖说明:

  • 创建版单的前提是要先登录系统
  • 领取版单需要获取前一个接口步骤返回的id

三、测试用例代码

1、登录接口用例

# NOTE: Generated By HttpRunner v3.1.5
# FROM: har\login.har


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


class TestCaseLogin(HttpRunner):

    config = Config("登录")\
        .verify(False)\
        .base_url("https://XXX.com")\    #在config定义url后,teststeps里就直接写接口路径就行了
        .variables(
            **{"username":"李白","psw":"123456"}      #在config定义变量,step里都可以用
        )\
        .export(*["userId","userNo","userName"])      #下面step提取参数设为变量后,在config里导出,好处是其他步骤跟用例都可以引用


    teststeps = [
        Step(
            RunRequest("登录接口")
            .post("/auth/login_by_password")  
            .with_headers(
                **{
                    "accept": "application/json, text/plain, */*",
                    "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36",
                    "content-type": "application/json;charset=UTF-8",
                    "accept-language": "zh-CN,zh;q=0.9",
                }
            )
            .with_json(
                {
                    "employeeName": "$username",    #引用config定义的变量
                    "loginType": "PASSWORD",
                    "password": "$psw",         #引用config定义的变量
                }
            )
            .extract()         #提取返回参数
            .with_jmespath("body.data.userId","userId")
            .with_jmespath("body.data.userNo","userNo")
            .with_jmespath("body.data.userName","userName")
            .validate()        #断言
            .assert_equal("status_code", 200)
            .assert_equal("body.successful", True)
            .assert_equal("body.code", "200")
            .assert_equal("body.message", "请求成功")
        ),
    ]


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

  

2、业务流程用例

# NOTE: Generated By HttpRunner v3.1.5
# FROM: har\received.har

from httprunner import HttpRunner, Config, Step, RunRequest, RunTestCase
from testcases.login_test import TestCaseLogin  #因为要引用登录用例,所以要先import

class TestCaseReceived(HttpRunner):

    config = Config("业务主流程")\
        .verify(False)\
        .base_url("https://XXX.cn")\
        .export(*["prototypeId"])

    teststeps = [
        Step(
        #调用另一个用例时,注意是用RunTestCase("自己随意命名").call(另一个用例的类名) RunTestCase("调用登录用例").call(TestCaseLogin).export(*["userId","userNo","userName"]) ), Step( RunRequest("创建版单接口") .post("/XXX/type/add") .with_headers( **{ "OSChannel": "web", "Accept": "application/json, text/plain, */*", "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36", "CURRENT-USER-ID": "100", "Content-Type": "application/json;charset=UTF-8", "CURRENT-USER-NAME": "%E6%E5%B9%B3", "Accept-Language": "zh-CN,zh;q=0.9", } ) .with_json( { "pictureList": [ { "url": "https://794802905c9a3699256.png", "pictureTypeEnum": "POSITIVE", }, ], "purchaserInfo": { "bdId": "100", }, "purchaserSource": "", "saleGroup": "国内-全国", "deliveryTime": "2021-07-16 00:00:00", "category": "男装-上装-衬衫-", "referType": "IMG", "referRemark": "同款复版", ) .extract() #提取返回参数,下面的领取接口需要用到这个id,注意要在config里export .with_jmespath("body.data.prototypeId","prototypeId") .validate() .assert_equal("status_code", 200) .assert_equal('headers."Content-Type"', "application/json") .assert_equal("body.successful", True) .assert_equal("body.code", "200") .assert_equal("body.message", "请求成功") ), Step( RunRequest("领取接口") .put("/XXXtype/received") .with_headers( **{ "Accept": "application/json, text/plain, */*", "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36", "CURRENT-USER-ID": "100", "Content-Type": "application/json;charset=UTF-8", "CURRENT-USER-NAME": "%EB7%E5%B9%B3", "Accept-Language": "zh-CN,zh;q=0.9",             } ) .with_json( { "prototypeId": "$prototypeId",  #直接$引用变量就行了 "currentUserId": "1078", "workerId": "1078", "currentUserName": "李白", "currentUserCode": "00878", } ) .validate() .assert_equal("status_code", 200) .assert_equal('headers."Content-Type"', "application/json") .assert_equal("body.successful", True) .assert_equal("body.code", "200") .assert_equal("body.message", "请求成功") .assert_equal("body.data", None) ), ] if __name__ == "__main__": TestCaseReceived().test_start()

  

  

标签:body,__,项目,json,equal,assert,application,HttpRunner3,实例
来源: https://www.cnblogs.com/Chilam007/p/15023405.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有