1.pytest框架 接口类测试方案: pytest框架环境搭建 pytest 结合Allure操作

接口类测试方案:

1.工具类:postman,jmeter,soapUI
2.代码类:
unittest:解释器自带框架
pytest:高效率,可定制化
nose:
RF:封装关键字
3.测试平台
前端
后端
执行机制--框架pytest

pytest框架环境搭建

1.使用pip安装pytest

pip install pytest
pip install pytest-html  原生态的报告模板

2.查看安装是否成功

pip show pytest

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

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

4.新建工程(规范化编程:分包分模块,分层分级写)
工程建立规范:lib(库),data(测试数据),testcase(测试用例),

5.数据驱动(参数化)
在方法前添加语法糖即可:

# @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

6.pytest的setup与teardown

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'])

pytest 结合Allure操作

  • Allure 安装
    1、下载Allure.zip并解压到任意目录(C:\allure\allure-2.13.0\)
    2、添加该路径到环境变量的path中
    3、cmd 安装 pip install allure-pytest
    4、验证是否安装成功:cmd 中输入allure查看
    5、allure报告生成
# 1、执行pytest单元测试,生成Allure报告, 数据存在/tmp目录
pytest -s --alluredir=../report/tmp # -s 表示允许执行print语句
# 2、执行命令,生成测试报告
allure generate ../report/tmp -o ..report/report --clean
  • allure 报告可以展示多级
    @allure.feature(‘一级标题’)
    @allure .story('二级标题')
    @allure.title(‘用例的标题’)
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')
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章