pytest測(cè)試報(bào)告美化

1、Pytest測(cè)試報(bào)告

pytest-HTML是一個(gè)插件,pytest用于生成測(cè)試結(jié)果的HTML報(bào)告。兼容Python 2.7,3.6

1、安裝pytest-HTML

安裝方式:pip install pytest-html

2、使用

然后寫一個(gè)測(cè)試類

在命令行中執(zhí)行:

py.test test_exmaple.py --html=./report.html

如下:

image.png

在當(dāng)前目錄下可以看到生成了一個(gè)report.html的文件,點(diǎn)開就可以看到測(cè)試報(bào)告啦

image.png

2、使用Pytest+Allure生成精美的測(cè)試報(bào)告

Allure是一款輕量級(jí)的開源測(cè)試報(bào)告框架。

1、安裝Allure

pip install allure-pytest

allure-pytest的官方文檔中詳細(xì)介紹了allure-pytest所具有的功能。

2、生成測(cè)試報(bào)告

寫一個(gè)測(cè)試類

3、執(zhí)行生成報(bào)告

執(zhí)行命令

  • 運(yùn)行測(cè)試用例
命令行:py.test  test_exmaple.py  --alluredir ./result/
代碼:pytest.main(['-s','test_exmaple.py','--alluredir','./result/']) 
  • 生成測(cè)試報(bào)告頁(yè)面

    通過下面的命令將./result/目錄下的測(cè)試數(shù)據(jù)生成測(cè)試報(bào)告頁(yè)面:

命令行:allure generate ./result/ -o ./report/ --clean
代碼:os.system('allure generate ./result/ -o ./report/ --clean')

在report文件夾下,打開index.html就可以看到比較精美的測(cè)試報(bào)告啦

image.png

3、Pytest+allure定制測(cè)試報(bào)告

在運(yùn)行測(cè)試用例時(shí)候,我們可以使用注解來生成精美的測(cè)試報(bào)告

image.png

案例:

使用yaml 數(shù)據(jù)驅(qū)動(dòng) 來進(jìn)行自動(dòng)化測(cè)試

1、case.yaml文件

#登錄的測(cè)試用例
loginCase:
  -
    title: 登錄成功的測(cè)試用例
    description: 測(cè)試電商系統(tǒng)的登錄,用戶名和密碼正確,登錄成功
    cases:
      -
        name: 打開登錄頁(yè)
        method: get
        url: http://www.testingedu.com.cn:8000/Home/user/login.html
      -
        name: 輸入用戶名
        method: input
        locator: id=username
        value: 13800138006
      -
        name: 輸入密碼
        method: input
        locator: id=password
        value: 123456

      -
        name: 輸入驗(yàn)證碼
        method: input
        locator: id=verify_code
        value: 1111
      -
        name: 點(diǎn)擊登錄按鈕
        method: click
        locator: name=sbtbutton

2、讀取yaml文件的:readYaml.py

import yaml
datas=None
# 讀取數(shù)據(jù)驅(qū)動(dòng)的數(shù)據(jù)
with open('G:/自動(dòng)化/autoWeb/lib/cases/case.yaml',encoding='utf-8') as f:
    datas =yaml.safe_load(f)

print(datas)

3、測(cè)試用例:test_One.py

import allure
import pytest

from autoWeb.data.readYaml import datas
from autoWeb.webKeys.webkeys import *
@allure.epic("電商項(xiàng)目自動(dòng)化")
@allure.feature("電商項(xiàng)目自動(dòng)化-登錄測(cè)試用例")
class Test_One:
    # 電商項(xiàng)目po封裝
    @allure.story("打開瀏覽器")
    def setup_class(self):
        self.web = WebKeys()
        self.web.open('chrome')
    @allure.story("進(jìn)行登錄成功用例")
    @pytest.mark.parametrize('listcases',datas['loginCase'])
    def test_login(self,listcases):
        allure.dynamic.title(listcases['title'])  #獲取標(biāo)題
        allure.dynamic.description(listcases['description']) #獲取描述
        testcase=listcases['cases']
        for case in testcase:
            listcase=list(case.values())
            with allure.step(listcase[0]):
                try:
                    func=getattr(self.web,listcase[1])
                    value=listcase[2:]
                    func(*value)
                except Exception as e:
                    # allure.attach() #獲取錯(cuò)誤截圖
                    pytest.fail("運(yùn)行失敗")

4、運(yùn)行用例的runner.py


import os

import pytest
from autoWeb.data import readYaml
print(readYaml)

# 調(diào)用pytest的main函數(shù)執(zhí)行測(cè)試用例
pytest.main(['-s','test_One.py','--alluredir','./result/'])
#生成allure測(cè)試報(bào)告
os.system('allure generate ./result/ -o ./report/ --clean')

運(yùn)行runner.py后 可以打開我們的測(cè)試報(bào)告,就可以看到詳細(xì)內(nèi)容的測(cè)試報(bào)告


image.png

說明:

@pytest.mark.parametrize("參數(shù)名",列表數(shù)據(jù))
參數(shù)名:用來接收每一項(xiàng)數(shù)據(jù),并作為測(cè)試用例的參數(shù)。
列表數(shù)據(jù):一組測(cè)試數(shù)據(jù)。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容