接口自动化数据关联

本文介绍如何在接口自动化测试中处理token认证,使用pytest框架进行接口封装优化,减少代码沉余。通过创建common工具包进行接口关联,并采用allure报告生成,最后讨论了如何将代码推送到gitee并结合jenkins实现持续集成。
摘要由CSDN通过智能技术生成

上干货   : token认证的接口

接口自动化涉及到的包  :request   框架:pytest

本节不用pymysql数据库进行数据校验了,此小段介绍一下思路 

创建common工具存放,sprict脚本层面,api封装层面

1.1  未封装大概思路

import request
class Test_xiangmu: 
      def test_xiangmu1_ok(self)
           url=""
           heard={}
           body={}
           resp=request.post(url=url,heards=heard,json=body)
           assert 200==resp.status.code
           assert    ==resp.json().get("")
      def test_xiangmu12_ok(self)
           url=""
           heard={}
           body={}
           resp=request.post(url=url,heards=heard,json=body)
           assert 200==resp.status.code
           assert    ==resp.json().get("") 
           

那么,在我们进行接口自动化的时候整个项目的代码沉余度是非常大的,利用封装思想对接口进行优化。

1.2 

import request
class API_xiangmu:
      @classmethod
      def api_xiangmu(cls,body)
          url=""
          head={}
          resp=request.post(url=url,heards=head,json=body)

代码的封装思路:api层面,sprict脚本层,对api封装之后为在脚本层面减少沉余

1.3

class ASSERR_xiangmu:
      @classmethod
      def assert_xiangmu1(cls,resp,status.code,message)
          assert status.code==resp.status.code
          assert message==resp.json().age()

1.4

放入common用于接口关联

class TOKEN_xiangmu:
      @classmethod
      def get_token(cls)
          .....
          resp=request.post(........)
          return resp.json().get("")

1.5

报告生成,allure形式所以这里改掉。

[pytest]
addopts = -s --alluredir report

1.6

import request
import allure
from API import API_xiangmu
from common import ASSERT
from common import TOKEN_xiangu1
class Test_xiangmu:
      def upset(self)
          self.tn=TOKEN_xiangu1.get_token()
      @allure.step(tiele="#test_name")
      @allure.severity(allure.severity_level.CRITICAL)#错误级别,报告方便查看    
      def test_xiangmu1_ok(self)
           body={}
           resp=API.api_xiangmu(body)
           ASSERT.assert1(200,ok)
      @allure.step(tiele="#test_name")
      @allure.severity(allure.severity_level.MINOR)#错误级别,报告方便查看    
      def test_update_ok(self)
           body={}
           resp=API.api_xiangmu(body,self.tn)
           ASSERT.assert1(200,resp,ok) 

1.6

 push到gitee里面结合jenkins持续集合。

这就是代码实现思想,总之接口测试,并非要用python来完成,按照个人习惯也可以使用其它平台来完成。

在使用 requests 进行接口自动化测试时,可以通过以下几种方式来关联接口数据: 1. 使用变量存储接口返回的数据:在发送接口请求后,可以将返回的数据保存到一个变量中,以便后续使用。例如: ```python import requests # 发送接口请求 response = requests.get('http://api.example.com/get_data') # 将返回的数据存储到变量中 data = response.json() # 使用变量中的数据 print(data['key']) ``` 2. 使用全局变量存储接口数据:如果需要在多个接口间共享数据,可以使用全局变量来存储接口数据。例如: ```python import requests # 定义全局变量 global_data = {} # 发送接口请求 response = requests.get('http://api.example.com/get_data') # 将返回的数据存储到全局变量中 global_data['key'] = response.json()['key'] # 在其他地方使用全局变量中的数据 print(global_data['key']) ``` 3. 使用类或对象进行数据关联:如果有多个接口需要关联数据,可以定义一个类或对象来存储和管理接口数据。例如: ```python import requests class ApiData: def __init__(self): self.data = {} def get_data(self): # 发送接口请求 response = requests.get('http://api.example.com/get_data') # 将返回的数据存储到对象属性中 self.data['key'] = response.json()['key'] def use_data(self): # 在其他地方使用对象属性中的数据 print(self.data['key']) # 创建对象 api = ApiData() # 获取接口数据 api.get_data() # 使用接口数据 api.use_data() ``` 以上是一些常见的方法来关联接口数据,根据具体情况选择适合的方式进行接口数据关联
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值