Pytest+Allure+Jenkins接口自动化项目实战(一)

本文介绍了使用Python的pytest框架、Allure报告和Jenkins集成进行接口自动化测试的项目实践。内容涵盖了代码目录结构、yaml配置文件、用例模板、请求方法、登录逻辑、SQL操作、日志记录、断言方法以及测试报告的生成。目前项目实现了基本功能,后续将完善环境独立运行等特性。
摘要由CSDN通过智能技术生成

       经过一周多时间,基于python+pytest+excel+allure框架的接口自动化测试初版已基本实现,包括基本配置读取、用例读取、用例执行、sql读取执行、前置数据准备、后置数据清理以及测试报告生成等,环境独立运行、项目独立运行、用例独立运行、jenkins集成、邮件发送暂未实现,再后期版本会再次推出,现在把整个框架设计思路和想法分享给大家来参考和借鉴。希望大家也能提供更好的思路和方法帮助我进行优化改进。

       实战项目是三端交互的项目,所以在设计思路上要考虑多项目如何交互,目前只写了1个项目的,其它2个项目都预留了位置,后期直接添加就可以,思路一样。

一、整个代码目录及介绍

common

request.py  封装post、get请求方法,供所有地方调用

login.py 封装各项目、各种方式的登录方法,供所有地方调用

readFile.py 封装读取yaml中配置文件、excel中测试用例方法,供所有地方调用

execSql.py 封装sql操作增、删、改、查方法,供所有地方调用

prefixSqlData.py 封装前置、后置公共sql,供所有地方调用

assertion.py 封装断言方法,供所有用例中调用

logs.py 封装日志方法,供所有地方调用

config

sql.yaml  前置数据sql语句、后置清理sql语句等

test.yaml  test环境数据库配置、接口域名、测试账号、登录接口数据等

uat.yaml  uat环境数据库配置、接口域名、测试账号、登录接口数据等

testcase.xls  测试用例等

testcase

项目1

    用例1、用例2

项目2

    用例1、用例2

testcase.xls  接口测试用例等

conftest.py  放置了登录获取token供所有用例调用

run_all_case.py 执行所有测试用例并生成测试报告

logs

每天的日志数据

report

html  测试报告index.html

二、yaml文件基本配置

项目一:
   url: 'https://www.baidu.com/'
   headers:
        Content-Type: 'application/json'
        Authorization: 'token'
   account:
     a_account: '17900000000'
     b_account: '18000000000'
     c_account: '19900000000'
     c_account1: '19900000001'

   c_login:
     method: post
     url: '/user/login'
     param:
        type: "7"
        login_from: 7
        mobile: "18888888888"
        code: "123456"
   c_sms_code:
     method: post
     url: '/1.0/users/login'



   mysql:
     host: 127.0.0.1
     user: test
     pwd: test
     test_db: user



项目二:
  url: 'https://www.baidu.com/'

三、yaml文件sql配置

项目一:
  查id:
    - select_list
    - select id from A.B where create_mobile={}
  查用户id:
    - select_list
    - select id from A.B where mobile={}
  查团队id:
    - select_list
    - select id from A.B where company_user_id=(select id from A.B where mobile={})
  查C端用户id:
    - select_list
    - select user_id from A.B where mobile in({})
  解除用户:
    - update
    - update A.B set status=2 where mobile={}

项目二:
  查id:
    - select_list
    - select id from A.B where create_mobile={}
  查用户id:
    - select_list
    - select id from A.B where mobile={}
  查团队id:
    - select_list
    - select id from A.B where company_user_id=(select id from A.B where mobile={})
  查C端用户id:
    - select_list
    - select user_id from A.B where mobile in({})
  解除用户:
    - update
    - update A.B set status=2 where mobile={}

四、读取yaml文件、excel用例文件

#!/usr/bin/env python
# _*_coding:utf-8_*_
import yaml,os,sys,xlwt,xlrd
from common.logs import Log

class ReadFile(object):
    log = Log()
    _instance=None
    def __new__(cls,*args,**kwargs):
        if cls._instance is None:
            cls._instance=super().__new__(cls)
        return cls._instance

    def __init__(self):
        self.excel_path = os.path.join(os.path.dirname(os.path.dirname(__file__)), 'config/testcase.xls')
        self.yaml_path = os.path.join(os.path.dirname(os.path.dirname(__file__)), 'config/test.yaml')
        self.sql_yaml_path = os.path.join(os.path.dirname(os.path.dirname(__file__)), 'config/sql.yaml')

    def read_yaml(self,path_type):
        """
        读yaml文件
        :return:
        """
        try:
            if path_type=='yaml_path':
                file_path=self.yaml_path
            elif path_type=='sql_yaml_path':
                file_path=self.sql_yaml_path

            with open(file_path,'r',encoding='utf-8') as f:
                return yaml.load(f.read())
        except Exception as e:
            self.log.error("读yaml文件报错{}".format(e))

    def read_excel(self,sheet_name,function,casename=None):
        """
        读取excel
        :param sheet_name:
        :param function:
        :return:
        """
        try:
            book=xlrd.open_workbook(self.excel_path)
            sheet=book.sheet_by_name(sheet_name)
            param=[]
            for i in range(0,sheet.nrows):
                if casename==None:
                    if sheet.row_values(i)[0]==
评论 11
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

王大力测试进阶之路

打赏博主喝瓶水吧!!!

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

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

打赏作者

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

抵扣说明:

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

余额充值