pytest04 pytest常用外掛程式

2021-10-17 08:58:55 字數 2032 閱讀 3832

02、生成測試報告 外掛程式

--安裝

pip install pytest-html

--生成測試報告:

pytest --html=測試報告路徑/***.html

03、在pytest.ini中加入報告生成命令

addopts = -s --html=test_hello.html

--直接執行 pytest即可

--如果這裡加上了 -s 則不會生成報告

--只有引數為addopts = --html=test_hello.html 才會生成報告

04、改變測試函式的執行順序 外掛程式

--pip install pytest-ordering  安裝執行順序外掛程式

--具體使用的測試執行案例如下:

import pytest

# 都是正數的話,越小越先執行,比如order=3, 2, 1的話,order為1的先執行

# 都是負數的話,值越小越先執行

# 0和負數的話,0先執行,剩下的數值越小越先執行

# 0和正數的話,0先執行,剩下的數越小越先執行

# 0 正數 負數,0先執行,正數再執行,負數最後

# 執行順序:0 > 較小的正數 > 較大的正數 > 較小的負數 > 較大的負數

@pytest.mark.run(order=3)

def test_a(): # 函式測試用例必須test開頭

print("\n----> test_a")

assert 1

@pytest.mark.run(order=-1)

def test_b():

print("\n----> test_b")

assert 1

@pytest.mark.run(order=0)

def test_c():

print("\n----> test_c")

assert 1

05、失敗重試外掛程式 – 重試全部指令碼

--pip install pytest-rerunfailures

--pytest --reruns n # n表示重新執行的次數,這個會把失敗的所有指令碼重新執行

--或者在pytest.ini中addopts引數上加入引數 --reruns 2 --reruns-delay m [重試時間為m秒]

import pytest

class test_abd:

def setup_class(self):

print("-----> setup_class")

def teardown_class(self):

print("----> teardown_class")

def test_a(self):

print("----> test_a")

assert 1

def test_b(self):

print("----> test_b")

assert 0

if __name__ == "__main__":

pytest.main("-s test_abc.py")

06、重試單個測試用例

import pytest

def test_a(): # 函式測試用例必須test開頭

print("\n----> test_a")

assert 1

def test_b():

print("\n----> test_b")

assert 1

# 這裡使用裝飾器 flaky ,失敗後重試5次

# 重試時間為1s

@pytest.mark.flaky(reruns=5, reruns_delay=1)

def test_c():

print("\n----> test_c")

assert 0

pytest (五) pytest中的斷言

pytest 的斷言報告,也很豐富,和詳情,比如 import pytest def test set comparison set1 set 1308 set2 set 8035 assert set1 set2 執行一下 有時候,我們需要對一些異常丟擲作斷言,可以用pytest.raises 比...

pytest原始碼 pytest原始碼目錄概要分析

pytest是什麼?pytest是乙個非常成熟的全功能的python測試框架。怎麼安裝?pip install pytest github專案位址在哪?pytest原始碼結構 在python安裝目錄的lib site packges下,分為pytest.py 和 pytest目錄 我們先看下pyte...

pytest(一)學pytest能做什麼

容易上手,入門簡單,文件豐富,pytest的官方文件中有很多的例項可以研究 能支援簡答的單元測試和複雜的功能測試 支援引數化parametrize 比unittest的ddt簡單 執行測試的時候可以將某些測試skip,或者對某些預期失敗的case標記成失敗 強大的fixture自定義功能,這是框架的...