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')
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章