接口测试关联讲解

38 篇文章 6 订阅
6 篇文章 0 订阅


1.通过类变量获取token

# 接口关联 实现登录 下单流程
# 代码 流程实现出来 框架中 实现这个流程 哪个知识点 实现好一点?
# unittest/pytest会要好点 普通写个用例 函数 管理用例
import jsonpath
import pytest
import requests


class TestCase:
    token = None
    openid = None
    sjproductid = None
    userid = None
    sjcartid = None
    # 响应 token号会变 响应回来的token号要提取出来 保存在变量中。放在一个地方,都能拿到token
    def test_login(self):
        url = 'http://39.98.138.157:5000/api/login'
        # 张三 token 2h
        data = {"password": "123456","username": "admin"}
        res = requests.post(url,json=data)
        print(res.json())
        # 提取token 保存在变量中 类变量 其他方式 2.session中去取 3.返回值 接口关联 要用的数据 提取出来 保存在变量中 类变量中 下个接口要用
        sjmsg = jsonpath.jsonpath(res.json(),'$.msg')[0]
        TestCase.token = jsonpath.jsonpath(res.json(),'$.token')[0]
        assert 'success' == sjmsg


    # 注意添加购物车下单的时候需要用到这里的userid和openid 这个怎么弄
    def test_getUserinfo(self):
        url = 'http://39.98.138.157:5000/api/getuserinfo'
        # 这个token不能写死 获得登录时的token号
        header = {"token":TestCase.token}
        res = requests.get(url,headers = header)
        # 获取所有的结果 userid和openid jsonpath 字典取值 jsonpath(数据,表达式)
        TestCase.openid = jsonpath.jsonpath(res.json(),'$..openid')[0]
        TestCase.userid = jsonpath.jsonpath(res.json(),'$..userid')[0]
        # 出现问题 分析问题 没有取到值 直接报错 刚开始的代码都是调通的 新加的代码 排查问题
        print('userid',TestCase.userid)
        print('openid',TestCase.openid)
        print(res.json())
        sjname = jsonpath.jsonpath(res.json(),'$..nikename')[0]
        assert '风清扬' == sjname

    # 选择商品
    def test_shopping(self):
        url = 'http://39.98.138.157:5000/api/getproductinfo?productid=8888'
        res = requests.get(url)
        print(res.json())
        TestCase.sjproductid = jsonpath.jsonpath(res.json(),'$..productid')[0]
        assert 8888 == TestCase.sjproductid

    # 添加购物车 为什么会失败 写法看着没有问题
    # 信息传入不正确, 值 有问题  请求头 没有问题  没有测试 参数有没有问题 传的值有没有问题
    # 确定一下 这个值是不是你想要 影响到你的判断 没有关联的代码注释掉
    # 更好的解决代码 理解这一块步骤 你写的代码是什么意思

    def test_cart(self):
        url = 'http://39.98.138.157:5000/api/addcart'
        header = {'token':TestCase.token}
        # 参数不能直接写死 userid openid 取userid的值 取openid的值 拿 哪里响应就去哪里拿
        # userid和openid拿到值了,还是报错
        data = {"openid": TestCase.openid,"productid": TestCase.sjproductid,"userid": TestCase.userid}
        res = requests.post(url,json = data,headers = header)
        print(res.json())
        #断言 debug用在跳页面的时候 习惯
        excartid = 45233
        TestCase.sjcartid = jsonpath.jsonpath(res.json(),'$..cartid')[0]
        print(TestCase.sjcartid)
        assert excartid == TestCase.sjcartid


    def test_order(self):
        url = 'http://39.98.138.157:5000/api/createorder'
        header = {'token':TestCase.token}
        data = {"cartid": TestCase.sjcartid, "openid": TestCase.openid, "productid": TestCase.sjproductid, "userid": TestCase.userid}
        res = requests.post(url,json=data,headers = header)
        print(res.json())



if __name__ == '__main__':
    pytest.main(['-sv','test_demo1.py'])

2.通过返回值获取token

import jsonpath
import pytest
import requests

# 上个接口的返回值,下个接口想要用
class TestCase:
    def test_login(self):
        url = 'http://39.98.138.157:5000/api/login'
        # 张三 token 2h
        data = {"password": "123456","username": "admin"}
        res = requests.post(url,json=data)
        print(res.json())
        # 提取token 保存在变量中 类变量 其他方式 2.session中去取 3.返回值 接口关联 要用的数据 提取出来 保存在变量中 类变量中 下个接口要用
        sjmsg = jsonpath.jsonpath(res.json(),'$.msg')[0]
        token = jsonpath.jsonpath(res.json(),'$.token')[0]
        assert 'success' == sjmsg
        return token

    # 注意添加购物车下单的时候需要用到这里的userid和openid 这个怎么弄
    # 获取个人信息 用token
    def test_getUserinfo(self):
        url = 'http://39.98.138.157:5000/api/getuserinfo'
        # 这个token不能写死 获得登录时的token号
        # header = {"token":self.test_login()}
        header = {"token":TestCase().test_login()}
        res = requests.get(url,headers = header)
        # 获取所有的结果 userid和openid jsonpath 字典取值 jsonpath(数据,表达式)
        TestCase.openid = jsonpath.jsonpath(res.json(),'$..openid')[0]
        TestCase.userid = jsonpath.jsonpath(res.json(),'$..userid')[0]
        # 出现问题 分析问题 没有取到值 直接报错 刚开始的代码都是调通的 新加的代码 排查问题
        print('userid',TestCase.userid)
        print('openid',TestCase.openid)
        print(res.json())
        sjname = jsonpath.jsonpath(res.json(),'$..nikename')[0]
        assert '风清扬' == sjname

if __name__ == '__main__':
    pytest.main(['-sv','test_demo2.py'])

3.把token放在session里

# 3.把token放在session中
# session:英文 开会 会议
# 编程:一次会话
# 登录--下单--支付 session
# 登录开始 会话开始建立 系统任何操作 session存放了会话过程中任何对象
# 浏览器关闭 session会话就结束了

# 登录进去了 token放在session里面 记录到我了 我在里面做的操作 都会知道是谁做的
# 你在session去访问 访问 登录 session
import jsonpath
import pytest
import requests

# 1.创建session对象
# 2.设置session的headers属性 token放进去
class TestCase:
    # token = None
    session = requests.session()
    openid = None
    sjproductid = None
    userid = None
    sjcartid = None
    # 响应 token号会变 响应回来的token号要提取出来 保存在变量中。放在一个地方,都能拿到token
    def test_login(self):
        url = 'http://39.98.138.157:5000/api/login'
        # 张三 token 2h
        data = {"password": "123456","username": "admin"}
        # res = requests.post(url,json=data)
        res=TestCase.session.post(url,json=data)
        print(res.json())
        # 提取token 保存在变量中 类变量 其他方式 2.session中去取 3.返回值 接口关联 要用的数据 提取出来 保存在变量中 类变量中 下个接口要用
        sjmsg = jsonpath.jsonpath(res.json(),'$.msg')[0]
        token = jsonpath.jsonpath(res.json(),'$.token')[0]
        # 把这个token放在session的headers里面 给头部里面设置属性 用update
        TestCase.session.headers.update({'token':token})
        assert 'success' == sjmsg

    # 获得个人信息,获得谁的个人信息 拿到token
    # 注意添加购物车下单的时候需要用到这里的userid和openid 这个怎么弄
    def test_getUserinfo(self):
        url = 'http://39.98.138.157:5000/api/getuserinfo'
        # 这个token不能写死 获得登录时的token号
        # header = {"token":TestCase.token}
        # res = requests.get(url,headers = header)
        # session头部里面已经有了token
        res = TestCase.session.get(url)
        # 获取所有的结果 userid和openid jsonpath 字典取值 jsonpath(数据,表达式)
        TestCase.openid = jsonpath.jsonpath(res.json(),'$..openid')[0]
        TestCase.userid = jsonpath.jsonpath(res.json(),'$..userid')[0]
        # 出现问题 分析问题 没有取到值 直接报错 刚开始的代码都是调通的 新加的代码 排查问题
        print('userid',TestCase.userid)
        print('openid',TestCase.openid)
        print(res.json())
        sjname = jsonpath.jsonpath(res.json(),'$..nikename')[0]
        assert '风清扬' == sjname
    #
    # # 选择商品
    def test_shopping(self):
        url = 'http://39.98.138.157:5000/api/getproductinfo?productid=8888'
        res = requests.get(url)
        print(res.json())
        TestCase.sjproductid = jsonpath.jsonpath(res.json(),'$..productid')[0]
        assert 8888 == TestCase.sjproductid
    #
    # # 添加购物车 为什么会失败 写法看着没有问题
    # # 信息传入不正确, 值 有问题  请求头 没有问题  没有测试 参数有没有问题 传的值有没有问题
    # # 确定一下 这个值是不是你想要 影响到你的判断 没有关联的代码注释掉
    # # 更好的解决代码 理解这一块步骤 你写的代码是什么意思
    #
    def test_cart(self):
        url = 'http://39.98.138.157:5000/api/addcart'
        # header = {'token':TestCase.token}
        # 参数不能直接写死 userid openid 取userid的值 取openid的值 拿 哪里响应就去哪里拿
        # userid和openid拿到值了,还是报错
        data = {"openid": TestCase.openid,"productid": TestCase.sjproductid,"userid": TestCase.userid}
        # res = requests.post(url,json = data,headers = header)
        res = TestCase.session.post(url,json=data)
        print(res.json())
        #断言 debug用在跳页面的时候 习惯
        excartid = 45233
        TestCase.sjcartid = jsonpath.jsonpath(res.json(),'$..cartid')[0]
        print(TestCase.sjcartid)
        assert excartid == TestCase.sjcartid
    #
    #
    def test_order(self):
        url = 'http://39.98.138.157:5000/api/createorder'
        # header = {'token':TestCase.token}
        data = {"cartid": TestCase.sjcartid, "openid": TestCase.openid, "productid": TestCase.sjproductid, "userid": TestCase.userid}
        # res = requests.post(url,json=data,headers = header)
        res = TestCase.session.post(url,json=data)
        print(res.json())



if __name__ == '__main__':
    pytest.main(['-sv','test_demo3.py'])

# 流程 改变的地方
# get post 一种方式 又能接受get又能接受post请求 封装请求方式

4.封装请求

# httpclient
# 专门封装getpost请求方式
import requests


class HttpClient:
    # 只要用post,get请求方式 就创建会话
    def __init__(self):
        self.session = requests.Session()

    # 发送请求 请求方式, 接口地址,接口参数类型,接口数据,头部,其他信息
    # 判断 你要发送的是请求方式 如果get post
    # post请求参数类型 json data
    def send_request(self,method,url,param_type=None,data=None,headers = None,**kwargs):
        # 请求方式转成大写
        method = method.upper()
        param_type = param_type.upper()
        print(method)
        if method == 'GET':
            response = self.session.request(method=method,url=url,params = data,**kwargs)
        elif method == 'POST':
            if param_type == 'FORM':
                response = self.session.request(method = method,url = url,data=data,**kwargs)
            else:
                response = self.session.request(method = method,url = url,json=data,**kwargs)
            pass
        elif method == 'DELETE':
            if param_type == 'FORM':
                response = self.session.request(method = method,url = url,data=data,**kwargs)
            else:
                response = self.session.request(method = method,url = url,json=data,**kwargs)
        elif method == 'PUT':
            if param_type == 'FORM':
                response = self.session.request(method = method,url = url,data=data,**kwargs)
            else:
                response = self.session.request(method = method,url = url,json=data,**kwargs)
        else:
            raise ValueError
        return response

    # 魔法方法 send_request 实例化类.方法名
    # 实例化类 HttpClient(),send_request()
    # a = HttpClient()
    def __call__(self,method,url,param_type,data=None,headers = None,**kwargs):
        return self.send_request(method,url,param_type,data,headers = None,**kwargs)

    def close_session(self):
        self.session.close()

# 3.把token放在session中
# session:英文 开会 会议
# 编程:一次会话
# 登录--下单--支付 session
# 登录开始 会话开始建立 系统任何操作 session存放了会话过程中任何对象
# 浏览器关闭 session会话就结束了

# 登录进去了 token放在session里面 记录到我了 我在里面做的操作 都会知道是谁做的
# 你在session去访问 访问 登录 session
import allure
import jsonpath
import pytest
import requests

# 1.创建session对象
# 2.设置session的headers属性 token放进去
from class40.httpclient import HttpClient

@allure.feature('接口自动化测试')
class TestCase:
    # token = None
    httpclient = HttpClient()
    openid = None
    sjproductid = None
    userid = None
    sjcartid = None
    # 响应 token号会变 响应回来的token号要提取出来 保存在变量中。放在一个地方,都能拿到token
    @allure.story('登录流程')
    @allure.title('登录')
    def test_login(self):
        url = 'http://39.98.138.157:5000/api/login'
        # 张三 token 2h
        data = {"password": "123456","username": "admin"}
        # res = requests.post(url,json=data)
        res=TestCase.httpclient(method = 'post',url = url,param_type='json',data=data )
        print(res.json())
        # 提取token 保存在变量中 类变量 其他方式 2.session中去取 3.返回值 接口关联 要用的数据 提取出来 保存在变量中 类变量中 下个接口要用
        sjmsg = jsonpath.jsonpath(res.json(),'$.msg')[0]
        token = jsonpath.jsonpath(res.json(),'$.token')[0]
        # 把这个token放在session的headers里面 给头部里面设置属性 用update
        TestCase.httpclient.session.headers.update({'token':token})
        assert 'success' == sjmsg

    # 获得个人信息,获得谁的个人信息 拿到token
    # 注意添加购物车下单的时候需要用到这里的userid和openid 这个怎么弄
    @allure.story('获得个人信息的流程')
    @allure.title('个人信息')
    def test_getUserinfo(self):
        url = 'http://39.98.138.157:5000/api/getuserinfo'
        # 这个token不能写死 获得登录时的token号
        # header = {"token":TestCase.token}
        # res = requests.get(url,headers = header)
        # session头部里面已经有了token
        # res = TestCase.session.get(url)
        res = TestCase.httpclient(url = url,method = 'get',param_type='json')
        # 获取所有的结果 userid和openid jsonpath 字典取值 jsonpath(数据,表达式)
        TestCase.openid = jsonpath.jsonpath(res.json(),'$..openid')[0]
        TestCase.userid = jsonpath.jsonpath(res.json(),'$..userid')[0]
        # 出现问题 分析问题 没有取到值 直接报错 刚开始的代码都是调通的 新加的代码 排查问题
        print('userid',TestCase.userid)
        print('openid',TestCase.openid)
        print(res.json())
        sjname = jsonpath.jsonpath(res.json(),'$..nikename')[0]
        assert '风清扬' == sjname
    #
    # # 选择商品 data=str 写法str 表单和json
    @allure.story('购物流程')
    @allure.title('购物')
    def test_shopping(self):
        url = 'http://39.98.138.157:5000/api/getproductinfo?productid=8888'
        # res = requests.get(url)
        res = TestCase.httpclient(url = url,method = 'get',param_type='json')
        print(res.json())
        TestCase.sjproductid = jsonpath.jsonpath(res.json(),'$..productid')[0]
        assert 8888 == TestCase.sjproductid
    #
    # # 添加购物车 为什么会失败 写法看着没有问题
    # # 信息传入不正确, 值 有问题  请求头 没有问题  没有测试 参数有没有问题 传的值有没有问题
    # # 确定一下 这个值是不是你想要 影响到你的判断 没有关联的代码注释掉
    # # 更好的解决代码 理解这一块步骤 你写的代码是什么意思
    #
    @allure.story('添加购物车流程')
    @allure.title('添加购物车')
    def test_cart(self):
        url = 'http://39.98.138.157:5000/api/addcart'
        # header = {'token':TestCase.token}
        # 参数不能直接写死 userid openid 取userid的值 取openid的值 拿 哪里响应就去哪里拿
        # userid和openid拿到值了,还是报错
        data = {"openid": TestCase.openid,"productid": TestCase.sjproductid,"userid": TestCase.userid}
        # res = requests.post(url,json = data,headers = header)
        # res = TestCase.session.post(url,json=data)
        res = TestCase.httpclient(method = 'post',url = url,param_type='json',data=data)
        print(res.json())
        #断言 debug用在跳页面的时候 习惯
        excartid = 45233
        TestCase.sjcartid = jsonpath.jsonpath(res.json(),'$..cartid')[0]
        print(TestCase.sjcartid)
        assert excartid == TestCase.sjcartid
    #
    #
    @allure.story('下单流程')
    @allure.title('下单')
    def test_order(self):
        url = 'http://39.98.138.157:5000/api/createorder'
        # header = {'token':TestCase.token}
        data = {"cartid": TestCase.sjcartid, "openid": TestCase.openid, "productid": TestCase.sjproductid, "userid": TestCase.userid}
        # res = requests.post(url,json=data,headers = header)
        # res = TestCase.session.post(url,json=data)
        res = TestCase.httpclient(method = 'post',url = url,param_type='json',data=data)
        print(res.json())



if __name__ == '__main__':
    pytest.main(['-sv','test_demo4.py'])

# 流程 改变的地方
# get post 一种方式 又能接受get又能接受post请求 封装请求方式

5.运行命令

import os

import pytest

if __name__ == '__main__':
    pytest.main(['-sv','test_demo4.py','--alluredir','./result','--clean-alluredir'])
    os.system('allure generate ./result -o ./reports --clean')
   
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

司小幽

真诚赞赏,手留余香。

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值