主頁(yè) > 知識(shí)庫(kù) > python單元測(cè)試之pytest的使用

python單元測(cè)試之pytest的使用

熱門標(biāo)簽:小蘇云呼電話機(jī)器人 市場(chǎng)上的電銷機(jī)器人 地圖標(biāo)注面積 北瀚ai電銷機(jī)器人官網(wǎng)手機(jī)版 儋州電話機(jī)器人 所得系統(tǒng)電梯怎樣主板設(shè)置外呼 朝陽(yáng)手機(jī)外呼系統(tǒng) 佛山400電話辦理 北京電銷外呼系統(tǒng)加盟

一、前提準(zhǔn)備

1、前提:需要安裝pytest和pytest-html(生成html測(cè)試報(bào)告)

pip install pytest 和 pip install pytest-html 

安裝插件:pip install 插件名

2、命名規(guī)范

 Pytest單元測(cè)試中的類名和方法名必須是以test開頭,執(zhí)行中只能找到test開頭的類和方法,比unittest更加嚴(yán)謹(jǐn)

Pytest: setup, setup_class 和 teardown, teardown_class 函數(shù) ( 和 unittest 執(zhí)行效果一樣 ) 運(yùn)行于測(cè)試方法的始末,即 : 運(yùn)行一次測(cè)試函數(shù)會(huì)運(yùn)行一次 setup 和 teardown 運(yùn)行于測(cè)試方法的始末 , 但是不管有多少測(cè)試函數(shù)都只執(zhí)行一次 setup_class 和 teardown_class

二、pytest生成自帶的html測(cè)試報(bào)告

前提條件:需要下載pytest-html模塊(python自帶的生成測(cè)試報(bào)告模塊)

pip install pytest-html

如果不安裝pytest-html會(huì)報(bào):

案例: 1)

pytest.main("模塊.py")【運(yùn)行指定模塊下,運(yùn)行所有test開頭的類和測(cè)試用例】 

 pytest.main(["--html=./report.html","模塊.py"])

import pytest
class Test():
    def test1(self):
        print("這是測(cè)試1")
    def test1(self):
        print("這是測(cè)試2")
if __name__ == '__main__':
    pytest.main(["--html=./report.html", "test_004.py"])

結(jié)果:

2)運(yùn)行指定模塊指定類指定用例,冒號(hào)分割,并生成測(cè)試報(bào)告

pytest.main([‘--html=./report.html',‘模塊.py::類::test_a_001'])

import pytest
class Test():
    def test1(self):
        print("這是測(cè)試1")
    def test2(self):
        print("這是測(cè)試2")
if __name__ == '__main__':
    pytest.main(["--html=./report.html", "test_004.py::Test::test1"])

結(jié)果:

3)直接執(zhí)行pytest.main() 【自動(dòng)查找當(dāng)前目錄下,以test 開頭的文件或者以test結(jié)尾的py文件】

pytest.main([‘--html=./report.html'])

語(yǔ)句: pytst.main(['-x','--html=./report.html','t12est000.py'])

-x出現(xiàn)一條測(cè)試用例失敗就退出測(cè)試
-s:顯示print內(nèi)容

三、pytest運(yùn)行方式

. 點(diǎn)號(hào),表示用例通過(guò)
F 表示失敗 Failure
E 表示用例中存在異常 Error

四、allure  

Allure是一款輕量級(jí)并且非常靈活的開源測(cè)試報(bào)告框架。 它支持絕大多數(shù)測(cè)試框架, 例如TestNG、Pytest、JUint等。它簡(jiǎn)單易用,易于集成  

1、Allure常用的幾個(gè)特性

@allure.feature # 用于描述被測(cè)試產(chǎn)品需求

@allure.story # 用于描述 feature 的用戶場(chǎng)景,即測(cè)試需求

with allure.step (): # 用于描述測(cè)試步驟,將會(huì)輸出到報(bào)告中

allure.attach # 用于向測(cè)試報(bào)告中輸入一些附加的信息,通常是一些測(cè)試數(shù)據(jù),截圖等

案例1:關(guān)于pytest與Allure生成html測(cè)試用例 rr.csv

2,3,5
5,6,11

readCsv

import csv  # 導(dǎo)入csv模塊
 
 
class ReadCsv():
    def read_csv(self):
        item = []  # 定義一個(gè)空列表
        c = csv.reader(open("../dataDemo/rr.csv", "r"))  # 得到csv文件對(duì)象
        for csv_i in c:
            item.append(csv_i)  # 將獲取的數(shù)據(jù)添加到列表中
        return item
 
 
r = ReadCsv()
print(r.read_csv())

開發(fā)代碼:

class Cale():
    def jia(self,a,b):
        c=a+b
        return c
    def jian(self,a,b):
        c=a-b
        return c
    def cheng(self,a,b):
        c=a*b
        return c
    def chu(self,a,b):
        c=a/b
        return c

生成html代碼:

import pytest
from pytest01.readDemo.readCsv import ReadCsv
from pytest01.demo.cale import Cale
import os
import allure
r=ReadCsv()
cc=r.read_csv()
d=Cale()
class Test():
    @allure.story("加法函數(shù)測(cè)試正確")
    def test001(self):
        for i in cc:
            dd=d.jia(int(i[0]),int(i[1]))
            assert dd==int(i[2])
if __name__ == '__main__':
    pytest.main(['--alluredir', 'report/result', 'test_02.py'])
    split = 'allure ' + 'generate ' + './report/result ' + '-o ' + './report/html ' + '--clean'
    os.system(split)

到此這篇關(guān)于python單元測(cè)試之pytest的使用的文章就介紹到這了,更多相關(guān)pytest的使用內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

您可能感興趣的文章:
  • python測(cè)試框架unittest和pytest區(qū)別
  • Python 測(cè)試框架unittest和pytest的優(yōu)劣
  • 詳解Pytest測(cè)試用例的執(zhí)行方法
  • 詳解如何使用Pytest進(jìn)行自動(dòng)化測(cè)試
  • Pytest測(cè)試框架基本使用方法詳解
  • Pytest執(zhí)行unittest TestSuite(測(cè)試套件)的實(shí)現(xiàn)方法

標(biāo)簽:金融催收 江蘇 龍巖 寧夏 云南 商丘 定西 酒泉

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《python單元測(cè)試之pytest的使用》,本文關(guān)鍵詞  python,單元,測(cè),試之,pytest,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《python單元測(cè)試之pytest的使用》相關(guān)的同類信息!
  • 本頁(yè)收集關(guān)于python單元測(cè)試之pytest的使用的相關(guān)信息資訊供網(wǎng)民參考!
  • 企业400电话

    智能AI客服机器人
    15000

    在线订购

    合计11份范本:公司章程+合伙协议+出资协议+合作协议+股权转让协议+增资扩股协议+股权激励+股东会决议+董事会决议

    推薦文章