Pytest之重運(yùn)行及生成測試報(bào)告

09
2020-09
今天距2021年113天
這是ITester軟件測試小棧第156次推文

點(diǎn)擊上方藍(lán)字“ITester軟件測試小棧“關(guān)注我,每周一、三、五早上 08:30準(zhǔn)時推送,每月不定期贈送技術(shù)書籍。
微信公眾號后臺回復(fù)“資源”、“測試工具包”領(lǐng)取測試資源,回復(fù)“微信群”一起進(jìn)群打怪。
本文2124字,閱讀約需6分鐘
重運(yùn)行
在做UI自動化,如Selenuim或者Appium時,遇到某些元素未能及時顯示,導(dǎo)致點(diǎn)擊失敗,如果加上重跑,那么將有效提高報(bào)告的準(zhǔn)確性。
在Pytest中,可以使用pytest-rerunfailures用來失敗用例重跑。
安裝命令:
pip install pytest-rerunfailures
@pytest.mark.flaky(reruns=重試次數(shù), reruns_delay=次數(shù)之間的延時設(shè)置(單位:秒))
import pytest
@pytest.mark.flaky(reruns=2)
def test_demo():
assert 3 == 4
import pytest
@pytest.mark.flaky(reruns=2,reruns_delay=2)
def test_demo():
assert 3 == 4
生成測試報(bào)告
pytest使用pytest-html插件生成測試報(bào)告。
① 安裝pytest-html
安裝命令:
pip install pytest-html
② 舉個栗子
現(xiàn)有用例如下:
test_demo.py
import pytest
@pytest.mark.flaky(reruns=2,reruns_delay=2)
def test_demo():
assert 3 == 4
class TestDemo:
def test_demo01(self):
print("這是test_demo01")
def test_demo02(self):
print("這是test_demo02")
命令行輸入:pytest --html=report.html
運(yùn)行之后,會生成report.html,用瀏覽器打開report.html,如下:

用pytest-html生成的報(bào)告稍微簡單和丑了一點(diǎn),我們可以使用更強(qiáng)大的工具allure,allure測試報(bào)告框架幫助你輕松實(shí)現(xiàn)"高大上"報(bào)告展示。
01 安裝allure
①從github上下載allure,下載傳送門為:
https://github.com/allure-framework/allure2/releases
如下所示,選擇”Download“下載即可:

② 下載完后,解壓到項(xiàng)目根目錄下。

③ 安裝allure插件
安裝命令:
pip install allure-pytest
④ 添加環(huán)境變量:將allure-2.13.5\bin的目錄路徑添加到環(huán)境變量。
02 舉個栗子
① 現(xiàn)有用例如下:
test_demo.py
import pytest
@pytest.mark.flaky(reruns=2,reruns_delay=2)
def test_demo():
assert 3 == 4
class TestDemo:
def test_demo01(self):
print("這是test_demo01")
def test_demo02(self):
print("這是test_demo02")
② 在項(xiàng)目根目錄下新建目錄report。

③ 在命令行輸入:pytest -s -q --alluredir ./report,在report目錄下會生成一些json格式的。

④ 在report目錄下,新建目錄html,用于存放html報(bào)告。
⑤ 命令行輸入allure generate ./report -o ./report/html,生成了最終的html報(bào)告。

⑥用瀏覽器打開index.html文件,效果如下:


個人微信:Cc2015123
添加請注明來意 :)
