接口自动化-框架搭建(Python+request+pytest+allure)

使用代码如何开展接口自动化测试。

选择自动化测试用例

业务流程优先,单接口靠后,功能稳定优先,变更频繁不选。

搭建自动化测试环境

(1)安装python编译器3.7版本以上--自行安装

(2)安装pycharm工作,方便编写和维护代码--自行安装

(3)安装request三方库,用于发送请求

(4)安装pytest三方库,用于编写测试用例

(5)安装allure,用于查看生成和查看测试报告

搭建自动化测试框架

(1)搭建基础框架项目目录结构

(2)通用功能类的封装,如数据库连接,excle读取等

(3)接口对象(业务类)封装与调用:接口API+Pytest框架编写测试脚本

(4)测试数据参数化(一般是针对单接口的),测试数据json,yaml,excle等

(5)用例组织运行,运行测试用例并生成测试报告

四 代码实现自动化

五 实现持续集成CICD

自行完成python和pycharm工具的安装后,我们接下来就是安装request,pytest,以及allure。

六 request安裝

request是一個三方的库,他的安装非常简单,如下

安装:python -m pip install requests

验证:pip show requests

request的使用步骤:导入包-->发送接口请求--->查看响应数据

  • request发送请求
python 复制代码
request.请求方法(url,params=None,data=None,json=None,header=None)
//请求方法一般为:get,post,put,delete
  • requests查看响应

查看状态码:response.status_code

json形式的响应内容获取:response.json()

文本形式的响应内容获取:response.text

查看请求url:response.url

查看响应头部字符编码:response.encoding

查看头信息:response.headers

查看cookie:response.cookie

简单示例:

python 复制代码
# 1 导包
import requests
# 2 发送请求
response = requests.get(url='www.baidu.com')
# 查看响应
print(response.status_code)
print(response.text)

七 pytest框架环境搭建

1.使用pip安装pytest

python 复制代码
pip install pytest  安装pytest
pip install pytest-html  原生态的报告模板

2.查看安装是否成功

python 复制代码
pip show pytest

3.pytest执行测试用例的规则

  • .py测试文件必须以test开头(或以test结尾)
  • 测试类必须以Test开头,且无init方法
  • 测试方法必须以test开头,def test_001()
  • 断言必须使用assert

4. 数据驱动(参数化)
数据驱动:data driver testing(DDT) ,在自动化测试中测试数据与功能函数相分离,单独存储,运行自动化测试用例时,框架会读取数据源中的数据,把数据作为参数传递到功能函数中。

由于一般测试用例覆盖多条不同输入,根据不同的前置条件选取多条数据执行多次同一功能函数,这样减少重复代码,不同输入条件之间的测试结果互相不受影响,这就是数据驱动。

在方法前添加语法糖即可实现参数化:

python 复制代码
# @pytest.mark.parameriza('变量名',[参数化数据])    ---单一参数
@pytest.mark.parameriza('a',[1,2,3])
def test_001(self,a):
  print('第一个测试用例')
  assert 1+1==a
# 说明:用例会执行三次(三组数据),a分别为1,2,3

# @pytest.mark.parameriza('变量名1,变量名2',[(value1.,value2),(value2,)]) ,多个参数
@pytest.mark.parameriza('a,b',[(1,2),(3,4),(5,6)])
def test_001(self,a,b):
  print('第一个测试用例')
  assert a+1==b
# 说明:用例会执行三次(三组数据),a分别为1,3,5,b分别为2,4,6

5.pytest的 setup 与 teardown

  • setup:前置条件,测试用例的前置条件
  • teardown: 后置条件,用例执行后,需要恢复测试环境
  • yield :在这个关键字之前的代码为setup部分,之后的代码为teardown部分
    在pytest中有四种setup和teardown
    1、setup_module & teardown_module:在整个测试用例所在的文件中所有的方法运行前和运行后运行,只会运行一次;
    2、setup_class和teardown_class:则在整个文件中的一个class中所有用例的前后运行;
    3、setup_method和teardown_method:在class内的每个方法运行前后运行;
    4、setup_function、teardown_function:在非class下属的每个测试方法的前后运行;
python 复制代码
import pytest
 @pytest.fixture(scope='session')  #装饰器,声明下面的函数是setup函数,缺省值为function级
 #scope可以加入参数scope='class',将级别改为class
 #scope可以加入参数scope='module',将级别改为module
 #scope='session'  使用这个级别时,将fixture的内容写到conftest.py文件中,目录下的所有文件都使用这个配置
 def fun1():
     print('开始')
     yield  #这个关键字之后的代码相当于teardown
     print('结束')
 def test_c01(fun1):
     assert 1==2
 if __name__ == '__main__':
     pytest.main(['conftest.py','-s'])

6、运行文件:

  • 运行并生成html测试报告:pytest 用例路径 --html=./report/result.html , 注意:--html= 没有空格.
  • 运行该测试模块:pytest test_login.py -s, -s 输出print信息
  • 用main()方法来运行:pytest.main(['当前用例路径','--html=测试报告/XX.html ']) ---> 运行并生成html测试报告
  • 通过allure生成测试报告:pytest.main([__file__, '-sv','--alluredir','./report/report','--clean-alluredir'])

八 Allure安装--pytest 结合Allure操作

  • Allure 安装
    1、下载Allure.zip并解压到任意目录(C:\allure\allure-2.13.0\)
    2、添加该路径到环境变量的path中
    3、cmd 安装 pip install allure-pytest
    4、验证是否安装成功:cmd 中输入allure --version查看盗版本信息
    如果安装不成功,可以在环境变量--系统变量--path中配置上allure的bin目录
    5、allure报告生成: cmd执行命令生成、pycharm的终端Terminal执行命令
    (1)方式1:pytest [测试文件] -s -q --alluredir=../report/tmp 生成Allure报告, 数据存在/tmp目录,--alluredir用于指定存储测试结果的路径;-s 表示允许执行print语句;
    (2)方式2:allure generate ../report/tmp -o ..report/tmp --clean 生成测试报告,--clean 覆盖路径
    6、查看测试报告
    (1)使用默认浏览器打开:allure serve ../report/tmp/
    (2)打开报告:allure open -h 127.0.0.1 -p 8883 ./report/

在cmd/终端中:测试用例的执行以及报告的生成如下:

python 复制代码
pytest test_feature_story.py --alluredir=./result/2  # 执行测试用例模块test_feature_story.py
allure generate ./result/2 -o ./report/2/ --clean  # 生成allure 测试报告
allure open -h 127.0.0.1 -p 8883 ./report/2 # 打开allure 测试报告

在自动化测试中,执行以及生成报告如下:在main.py模块中,并运行可自动生成报告

python 复制代码
pytest.main(["./test_script", "-sv","--alluredir","./report/temp_jsonreport"]) 
os.system("allure generate ./report/temp_jsonreport -o ./report/html --clean")

# ./test_script   测试用例的路径,可以吧多个测试用例写在这个一个文件中
# --alluredir      创建allure报告的路径
# -o是执行
# --clean是清除之前生成的报告
# 或者
pytest.main([__file__, '-sv','--alluredir','./report/report','--clean-alluredir'])     
os.system('allure serve ./report/report')  #  

注意:因为allure生成的报告是json格式的,需要再转化成html格式的,所以会自动生成一个temp_jsonreport文件

  • allure 报告可以展示多级
    @allure.epic('1')
    @allure.feature('2')
    @allure .story('3')
    @allure.title('4')

例子:简单生成报告的过程

python 复制代码
import pytest 
import allure 
import os
@allure.epic('项目名称') 
@allure.feature('业务模块名称') 
class Test100:     
  @allure.story('接口名称')    
  @allure.title('用例标题1')    
  def test_c100(self):         
    assert 1 == 2     
  @allure.story('接口名称2')     
  @allure.title('用例标题2')     
  def test_c101(self):         
    assert 1 == 1 

if __name__ == '__main__':     
  pytest.main([__file__, '-sv','--alluredir','./report/report','--clean-alluredir'])     
  os.system('allure serve ./report/report')
相关推荐
fangzhanpeng1686 天前
快速掌握C#语言基础知识点(1.基础数据类型)
开发语言·c#·1024程序员节
无己心8 天前
面向多平台 AI 搜索引擎的适配层架构设计
前端·人工智能·搜索引擎·ai·1024程序员节
u01030552714 天前
AI驱动的安卓UI自动生成功能实现
人工智能·1024程序员节
u01030552715 天前
长株潭智能体赋能腾讯元器链接共享
1024程序员节
云贝贝贝16 天前
OceanBase认证体系介绍与备考指南
运维·服务器·oceanbase·1024程序员节
u01030552716 天前
Java图像处理实战指南
人工智能·1024程序员节
u01030552717 天前
Java AWT鼠标事件全解析
人工智能·1024程序员节
u01030552719 天前
Java实用类应用精讲
人工智能·1024程序员节
u01030552719 天前
Java I/O核心操作实战
人工智能·1024程序员节
u01030552721 天前
ArrayList操作详解与实战应用
人工智能·1024程序员节