基于Python+requests搭建的自动化框架-实现流程化的接口串联

框架产生目的:公司走的是敏捷开发模式,编写这种框架是为了能够满足当前这种发展模式,用于前后端联调之前(后端开发完接口,前端还没有将业务处理完毕的时候)以及日后回归阶段,方便为自己腾出学(mo)习(yu)时间。

鄙人一介小测试,一手承接产品,一手面对开发(4个后端,3个web前端,外加不知道几个的乙方),项目经手了5批人,鄙人是第5批的测试,规范没有,前人积累没有,天崩开局/(ㄒoㄒ)/~~。

老规矩,上图:

基于Python+requests搭建的自动化框架-实现流程化的接口串联插图

getpathInfo.py:获取autoapi文件夹所在的目录,便于项目换家,有没有其实无所谓的;

import os


def get_path():
    path = os.path.split(os.path.realpath(__file__))[0]
    return path


if __name__ == '__main__':
    print('测试路径是否OK,路径为:', get_path())

readConfig.py

import os
import configparser
import getpathInfo

path = getpathInfo.get_path()  
config_path = os.path.join(path, 'config')  
config = configparser.ConfigParser()  # 调用外部的读取配置文件的方法
config.read(config_path, encoding='utf-8')


class ReadConfig():

    def get_http(self, name):
        value = config.get('HTTP', name)
        return value

    def get_email(self, name):
        value = config.get('EMAIL', name)
        return value

    def get_account(self, name):
        value = config.get('ACCOUNT', name)
        return value


environment = ReadConfig().get_http('environment')  # 切换环境
mobile_phone = ReadConfig().get_account('mobile_phone')
password = ReadConfig().get_account('password')
parent_username = ReadConfig().get_account('parent_phone')
parent_pwd = ReadConfig().get_account('parent_pwd')

common:公共方法文件夹,最省事配置:日志封装(中文日志在这里要留意,有坑),请求封装(如果有token之类的限制的话)

logs.py

import logging
import time

from getpathInfo import get_path


class Log(object):
    def __init__(self, logger=None):
        """
        指定保存日志的文件路径,日志级别,以及调用文件
        将日志存入到指定的文件中
        """

        # 创建一个logger
        self.logger = logging.getLogger(logger)
        self.logger.setLevel(logging.DEBUG)
        # 创建一个handler,用于写入日志文件
        self.log_time = time.strftime('%Y-%m_%d_')
        self.log_path = get_path()+'\log\'
        self.log_name = self.log_path + self.log_time + 'test.log'
        fh = logging.FileHandler(self.log_name, 'a', encoding='utf-8')  # 追加模式,防止中文乱码
        fh.setLevel(logging.INFO)

        # 再创建一个handler,用于输出到控制台
        ch = logging.StreamHandler()
        ch.setLevel(logging.INFO)

        # 定义一个handler的输出样式
        formatter = logging.Formatter(
            '[%(asctime)s] %(filename)s->%(funcName)s line:%(lineno)d [%(levelname)s]%(message)s')
        fh.setFormatter(formatter)
        ch.setFormatter(formatter)

        # 给logger添加handler
        self.logger.addHandler(fh)
        self.logger.addHandler(ch)

        #  添加下面一句,在记录日志之后移除句柄
        fh.close()
        ch.close()

    def getLog(self):
        return self.logger


log = Log().getLog()
if __name__ == '__main__':
    log.info('日志')  # 抄过来的代码一定要进行测试

configHttp.py

这里是默认加上token的,但是并不是所有接口都是需要token,解决思路:再加个configHttpNormal.py,或者run_main()里再加个入参。

import json
import requests

from common.logs import log
from readConfig import environment,mobile_phone,password
from case.api.User import login
import urllib3
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)  # 如果有抓包需求,需要把verify设置成False,这行代码忽略系统之后的警告
"""
这个文件主要来通过get、post等方法来进行http请求,并拿到请求响应
"""

token = login(mobile_phone,password) # 登录方法
headers = {'Authorization': token,
           "Content-Type": 'application/json;charset=UTF-8',
           'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.106 Safari/537.36'}


class RunMain():
    def send_post(self, path, data):
        response = requests.post(url=environment+path,data=json.dumps(data,ensure_ascii=False,separators=(',',':')).encode('utf-8'), headers=headers,verify=False)
        log.info(path)  # path
        log.info(data)  # 请求,便于复现
        log.info(response.status_code)  # 请求状态码
        return response

    def send_get(self, path, data):
        response = requests.get(url=environment+path, data=data, headers=headers,verify=False)
        log.info(path)
        log.info(data)
        log.info(response.status_code)
        return response

    def run_main(self, method, path=None, data=None):
        result = None
        if method.lower() == 'post':
            result = self.send_post(path, data)
        elif method.lower() == 'get':
            result = self.send_get(path, data)
        else:
            print("method值错误!!!")
        return result


if __name__ == '__main__':
    result1 = RunMain().run_main('get', '/v1/wrong-work/info?type=1').json()
    result2 = RunMain().run_main('Get', '/v1/wrong-work/info?type=2').json()
    print(result1)
    print(result2)

case:用例文件夹,敏捷模式中开发提测基本上都是单接口提测,而且同一个模块下的接口分给了不同的开发,为了方便定位到开发,所以我将平台所有的接口都写了过来╮(╯▽╰)╭,

老规矩,下方直接执行,可以进行单接口的相关验证。

基于Python+requests搭建的自动化框架-实现流程化的接口串联插图(1)

 

相信各位也看了很多的接口自动化测试框架,我上面的应该没有多少新意,下面就是不同点;

regression_testing 顾名思义,回归测试ing,只有进行时,没有完成时……

鄙人将前端的业务逻辑通过接口层模拟了过来,做了许久之后,不禁产生了怀疑:这都是哪位鬼才设计的?没有被开发XX了吗?

基于Python+requests搭建的自动化框架-实现流程化的接口串联插图(2)

 

上图是使用unittest框架进行的,用例之间传参用法其实就是我上面展示的一部分;

其实这个文件夹下不适合使用unittest框架,因为通过用例之间的巧妙设计,虽然可以将业务之间的调用状态都模拟出来,但是由于脏数据的存在,某些bug没有在这些测试过程中表示出来。

哎,苦命啊,前端赶紧进行插桩操作,报错日志上传起来;鄙人只能放大招了,拾起了爬虫思路,自动遍历吧(穷举);

放代码片段,哎,遍历一时爽,重构火葬场(json目录深了之后,是真的不想看)。

基于Python+requests搭建的自动化框架-实现流程化的接口串联插图(3)

 

之后优化思路:将regression_testing里面的方法改成test_*,写一个run方法,加上测试报告,邮件通知(ps:但鄙人不用啊,就十个人,遇到bug,tapd复制上参数,“老哥,出bug了”)

 

参考文献:

自动化测试框架:

https://blog.csdn.net/songlh1234/article/details/84317617   # 框架主体都是这位老哥的,文抄公的水平您还满意?

https://www.jianshu.com/p/9d3f991c901a

https://www.cnblogs.com/peter200-OK/p/9086087.html   # 这个慎重,或许是鄙人太菜了,没有抄好这个作业

框架期间问题解决:

unittest用例间传参:

【python接口自动化框架-unittest】如何传参数到下一个case – WANG-X – 博客园 (cnblogs.com)

部分请求如: /v1/verify?clazz=1,2,3,4,5 解决方案如下:

python-map的用法 – lincappu – 博客园 (cnblogs.com)

原创文章,作者:迷途资源,如若转载,请注明出处:https://www.mipng.com/284.html

这些信息可能会帮助到你: 下载帮助 | 报毒说明 | 进站必看

修改版本安卓软件,加群提示为修改者自留,非本站信息,注意鉴别

发表评论

邮箱地址不会被公开。 必填项已用*标注

评论列表(5条)

  • Crystle Alonso
    Crystle Alonso 2021年2月24日 上午3:03

    Do you need more clients?

    We have amazing databases starting at $9.99 until the end of the Month!

    Visit us at StarDataGroup.com

  • Regina Schwab
    Regina Schwab 2021年4月1日 上午4:21

    What is Link Bypass?

    Link Bypass allows you to easily bypass any sales page and generate a new buy link with no coding required and just a few clicks of your mouse.

    How does Link Bypass work?

    Step #1 – Choose any sales page you want to bypass and link bypass with automatically create a custom payment link

    Step #2 – Add your new link to any landing page, sales page, or email with a few clicks of your mouse (Use your favorite page-building software or simply ‘copy and paste’)

    Step #3 – Boost conversions and make more money on affiliate promotions

    Visit us: https://warriorplus.com/o2/a/q8rmt/0

  • Cesar Jacquez
    Cesar Jacquez 2021年4月8日 上午4:34

    Hey,

    It was nice speaking to you the other day, this is the service I was telling you about that helped us boost our ROI almost 2000%

    Its a company called Lifemail.studio sorry it took so long to get back to you. They allow you to send any email doesn’t matter what.

    We dealt with a guy named Michael, he was friendly and got us setup really quickly.

    Regards,
    Cesar

  • Rolando Goodlet
    Rolando Goodlet 2021年6月2日 上午6:40

    Hello,

    It is with sad regret to inform you that BestLocalData.com is shutting down.

    We have made all our databases for sale for a once-off price.

    Visit our website to get the best bargain of your life. BestLocalData.com

    Regards,
    Rolando

  • Stephaine Melton
    Stephaine Melton 2021年6月29日 上午3:59

    It is with sad regret to inform you that because of the Covid pandemic BestLocalData.com is shutting down at the end of the month.

    We have lost family members and colleagues and have decided to shut down BestLocalData.com

    It was a pleasure serving you all these years. We have made all our databases available for $99 (All of it for $99) for those interested.

    Kind Regards,
    BestLocalData.com
    Stephaine