pytest文檔29-allure-pytest(最新最全,保證能搞成功!)


前言

之前寫了個pytest的allure相關的教程,只是停留在環境搭建完成,后續一直沒用,小編一直不喜歡這種花里胡哨的報告。
沒辦法,領導就喜歡這種,小伙伴們也喜歡,所以還是得把allure用起來,年底了,給領導一個漂亮的報告,也好加雞腿
之前用的是pytest-allure-adaptor插件,一年多前寫的了,很古老了,跟不上時代了,不能吸引當代的小青年,這里用最新的allure-pytest,符合當代小青年的口味。

allure-pytest 環境准備

windows環境相關:

python 3.6版本
pytest 4.5.0版本
allure-pytest 2.8.6 最新版

使用pip安裝pytest和allure-pytest,加上--index-url地址,下載會快一些

pip install pytest4.5.0 --index-url https://pypi.douban.com/simple
pip install allure-pytest
2.8.6 --index-url https://pypi.douban.com/simple

安裝完成之后,打開一個你之前寫的pytest腳本,看能不正常使用,如果報錯:AttributeError: module 'allure' has no attribute 'severity_level'
這個是之前 pytest-allure-adaptor 這個插件與 allure-pytest 不能共存,卸載掉 pytest-allure-adaptor

pip uninstall pytest-allure-adaptor

allure命令行工具

allure是一個命令行工具,需要去github上下載最新版https://github.com/allure-framework/allure2/releases

下載完成之后,解壓到本地電腦

把bin目錄添加到環境變量Path下

用例demo

conftest.py內容

import pytest


@pytest.fixture(scope="session")
def login():
    print("用例先登錄")

test_allure_demo.py內容

import allure
import pytest

@allure.step("步驟1:點xxx")
def step_1():
    print("111")

@allure.step("步驟2:點xxx")
def step_2():
    print("222")

@allure.feature("編輯頁面")
class TestEditPage():
    '''編輯頁面'''

    @allure.story("這是一個xxx的用例")
    def test_1(self, login):
        '''用例描述:先登錄,再去執行xxx'''
        step_1()
        step_2()
        print("xxx")


    @allure.story("打開a頁面")
    def test_2(self, login):
        '''用例描述:先登錄,再去執行yyy'''
        print("yyy")

運行用例

cd到test_allure_demo.py所在的目錄文件,命令行執行

pytest --alluredir ./report/allure_raw

D:\soft\code\xuexipytest>pytest --alluredir ./report/allure_raw
============================= test session starts =============================
platform win32 -- Python 3.6.0, pytest-4.5.0, py-1.5.4, pluggy-0.13.1
rootdir: D:\soft\code\xuexipytest
plugins: allure-pytest-2.8.6, forked-0.2, html-1.19.0, metadata-1.7.0, repeat-0.7.0, xdist-1.23.2
collected 9 items

case\test_allure_demo.py ..                                              [ 22%]
case\test_x.py ......                                                    [ 88%]
case\test_y.py .                                                         [100%]

============================== 9 passed in 0.21s ==============================

執行完成后,在當前目錄下,report目錄會生成一個allure_raw的原始文件,這個只是測試報告的原始文件,不能打開成html的報告

打開html的報告需要啟動allure服務,啟動命令如下

allure serve report/allure_raw

啟動服務,它會自動給個端口,直接用默認瀏覽器打開了

D:\soft\code\xuexipytest>allure serve report/allure_raw
Generating report to temp directory...
Report successfully generated to C:\Users\dell\AppData\Local\Temp\6056757827461248074\allure-report
Starting web server...
2019-12-08 00:41:09.921:INFO::main: Logging initialized @2228ms to org.eclipse.jetty.util.log.StdErrLog
Server started at <http://192.168.1.125:35346/>. Press <Ctrl+C> to exit

查看報告

瀏覽器上打開的報告內容

點 EN 按鈕可以查看中文報告

打開測試套件,可以查看報告的詳情,顯示的還是很詳細的


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM