API接口自动化测试

本节介绍,使用python实现接口自动化实现。

思路:讲接口数据存放在excel文档中,读取excel数据,将每一行数据存放在一个个列表当中。然后获取URL,header,请求体等数据,进行请求发送。

结构如下

excel文档内容如下:

一、Common与Config包

Config里面的config.ini主要存放的默认的路径内容等,如excel文件的地址,工作簿名称

Common里面主要是一些通用的方法,目前只需要读取config里面的数据

复制代码
import configparser

import os.path

#读取config里面的数据

class ReadConfig:

    def __init__(self):

        self.filePath = os.path.abspath(os.path.dirname(os.path.dirname(__file__)))+"\\config\\config.ini"

        print(self.filePath)

        self.config = configparser.ConfigParser()

        self.config.read(self.filePath,encoding='utf-8')

    def get_data(self,section,key):

        return self.config.get(section,key)

    def get_list(self,section):

        return self.config.items(section)

if __name__ == '__main__':

    conf = ReadConfig()

    print(conf.get_data("excel", "file_path"))

二、excel包

主要是实现读取excel表格数据,此处用的是openpyxl进行实现

复制代码
import openpyxl

class ReadExcel:

    def __init__(self,excel_path,sheet_name):

        self.excel_path = excel_path

        self.sheet_name = sheet_name

    def get_data(self,row):

        workbook = openpyxl.load_workbook(self.excel_path)

        sh = workbook[self.sheet_name]

        data = []

        for c in list(sh.rows)[row]:

            data.append(c.value)

        return data

if __name__ == '__main__':

    ex = ReadExcel('request.xlsx','Sheet1')

    print(ex.get_data(0))

    print(ex.get_data(1))

    print(ex.get_data(2))

    print(ex.get_data(3))

    print(ex.get_data(4))

三、requests包

主要用于发送请求,这里只写了常用的get post请求,需要可以加其他的。

由于post请求体有多种,此处只区分了两种,其他的可以加上。

复制代码
import json

import requests

class Request:

    def get_url(self,url,headers=None,paras=None):

        if url==None:

            print("URL地址为空")

        elif paras == None:

            r = requests.get(url,headers=headers,params=paras)

        else:

            r = requests.get(url, headers=headers, params=json.loads(paras))

        return r

    def post_url(self,url,content_type,headers=None,payload=None):

        if url==None:

            print("URL地址为空")

        else:

            if content_type == "application/json":

               payload_json = json.dumps(payload)

               r = requests.post(url,headers=headers,data=payload_json)

            elif content_type =="application/x-www-form-urlencoded":

                r = requests.post(url,headers=headers,data=payload)

            else:

                print("no this content-type")

            return r

    def choose_method(self,method,url,content_type,headers=None,payload=None):

        if method == "get":

            return self.get_url(url,headers,payload)

        elif method == "post":

            return self.post_url(url,content_type,headers,payload)

        else:

            print("no this method request")

四、testcases包

使用pytest框架进行自动化测试

复制代码
import json

import pytest

from autoStruct.apiAuto.common.requests import Request

from autoStruct.apiAutoTesting.common.readConfig import ReadConfig

from autoStruct.apiAutoTesting.excel.readExcel import ReadExcel

class TestApi:

    def setup_class(self):

        file_path = ReadConfig().get_data('excel', 'file_path')

        sheet_name = ReadConfig().get_data('excel', 'sheet_name')

        self.ex = ReadExcel(file_path, sheet_name)

    @pytest.mark.parametrize('num',[1,2,3,4,5])

    def testcase(self,num):

            data = self.ex.get_data(num)

            print(data)

            if data[3]==None:

                r = Request().choose_method(data[1],data[0],data[4],json.loads(data[2]),data[3])

            else:

                r = Request().choose_method(data[1], data[0], data[4], json.loads(data[2]), json.loads(data[3]))

            print(r.text)

if __name__ == '__main__':

    pytest.main(['-vs','testapi.py'])
学习安排上

作为一位过来人也是希望大家少走一些弯路,如果你不想再体验一次学习时找不到资料,没人解答问题,坚持几天便放弃的感受的话,在这里我给大家分享一些自动化测试的学习资源,希望能给你前进的路上带来帮助。【保证100%免费】

视频文档获取方式:

这份文档和视频资料,对于想从事【软件测试】的朋友来说应该是最全面最完整的备战仓库,这个仓库也陪伴我走过了最艰难的路程,希望也能帮助到你!以上均可以分享,点下方进群即可自行领取。

相关推荐
站大爷IP25 分钟前
Python文件操作的"保险箱":with语句深度实战指南
python
运器12331 分钟前
【一起来学AI大模型】算法核心:数组/哈希表/树/排序/动态规划(LeetCode精练)
开发语言·人工智能·python·算法·ai·散列表·ai编程
whoarethenext1 小时前
使用 C++ 实现 MFCC 特征提取与说话人识别系统
开发语言·c++·语音识别·mfcc
ITfeib1 小时前
Flutter
开发语言·javascript·flutter
想躺平的咸鱼干2 小时前
Volatile解决指令重排和单例模式
java·开发语言·单例模式·线程·并发编程
Owen_Q2 小时前
Denso Create Programming Contest 2025(AtCoder Beginner Contest 413)
开发语言·算法·职场和发展
·云扬·2 小时前
【Java源码阅读系列37】深度解读Java BufferedReader 源码
java·开发语言
liulilittle3 小时前
C++ i386/AMD64平台汇编指令对齐长度获取实现
c语言·开发语言·汇编·c++
巴里巴气3 小时前
selenium基础知识 和 模拟登录selenium版本
爬虫·python·selenium·爬虫模拟登录
19893 小时前
【零基础学AI】第26讲:循环神经网络(RNN)与LSTM - 文本生成
人工智能·python·rnn·神经网络·机器学习·tensorflow·lstm