最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

python自動化之如何利用allure生成測試報告

 更新時間:2021年05月02日 09:23:07   作者:塵世風(fēng)  
這篇文章主要給大家介紹了關(guān)于python自動化之如何利用allure生成測試報告的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

Allure測試報告框架幫助你輕松實現(xiàn)”高大上”報告展示。本文通過示例演示如何從0到1集成Allure測試框架。重點展示了如何將Allure集成到已有的自動化測試工程中、以及如何實現(xiàn)報表的優(yōu)化展示。Allure非常強大,支持多種語言多種測試框架,無論是Java/Python還是Junit/TestNG,其他語言或者框架實現(xiàn)的流程和本文一致,具體配置參照各語言框架規(guī)范

安裝

安裝allure

Windows用戶:

  • scoop install allure    (需要先下載并安裝Scoop,該方法無需配置環(huán)境變量)

MAC用戶:

  • 通過Homebrew進行自動安裝
  • brew install allure   (如果Homebrew版本比較舊,需要先升級Homebrew,否則安裝的allure版本也很老,可能會與Python插件不兼容)

手動安裝:

下載后解壓并配置環(huán)境變量

安裝allure-pytest插件

  • pip install allure-pytest

allure常用特性

希望在報告中看到測試功能,子功能或場景,測試步驟,包括測試附加信息可以使用@feature,@story,@step,@attach

步驟:

  • import allure
  • 功能上加@allure.feature("功能名稱")
  • 子功能上加@allure.story("子功能名稱")
  • 步驟上加@allure.step("步驟細節(jié)")
  • @allure.attach("具體文本信息"),需要附加的信息,可以是數(shù)據(jù),文本,圖片,視頻,網(wǎng)頁
  • 如果只測試部分功能運行的時候可以加限制過濾:
    • pytest 文件名 --allure-features "需要運行的功能名稱"

allure特性—feature/story

@allure.feature與@allure.store的關(guān)系

  • feature相當于一個功能,一個大的模塊,將case分類到某個feature中,報告中在behaviore中顯示,相當于testsuite
  • story相當于對應(yīng)這個功能或者模塊下的不同場景,分支功能,屬于feature之下的結(jié)構(gòu),報告在features中顯示,相當于testcase
  • feature與story類似于父與子關(guān)系

step特性

  • 測試過程中每個步驟,一般放在具體邏輯方法中
  • 可以放在關(guān)鍵步驟中,在報告中顯示
  • 在app,web自動化測試中,建議每切換到一個新的頁面當做一個step
  • 用法:
    • @allure.step() 只能以裝飾器的形式放在類或方法上面
    • with allure.step():  可以放在測試用例方法里面,但測試步驟的代碼需要被該語句包含

運行:

  在測試執(zhí)行期間收集結(jié)果

  pytest [測試文件] -s -q --alluredir=./result --clean-alluredir

  • --alluredir這個選項,用于指定存儲測試結(jié)果的路徑
  • --clean-alluredir 這個選項用來清除之前生成的結(jié)果

查看測試報告:

  方法一:測試完成后查看實際報告,在線看報告,會直接打開默認瀏覽器展示當前報告

      allure serve ./result

  方法二:從結(jié)果生成報告,這是一個啟動tomcat的服務(wù),需要兩個步驟

      生成報告:

          allure generate ./result -o ./report --clean   (注意:--clean用來清除之前已生成的報告)

      打開報告:

          allure open -h 127.0.0.1 -p 8883 ./report   (該方法直接生成一個tomcat服務(wù),可遠程訪問)

舉個例子:

有如下代碼文件

#!/usr/bin/python
# -*- coding: UTF-8 -*-
"""
@author:chenshifeng
@file:test_allure.py
@time:2020/10/10
"""
import allure
import pytest


@allure.feature('登錄模塊')
class TestLogin():
    @allure.story('登錄成功')
    @allure.title('登錄成功標題')
    def test_login_sucess(self):
        with allure.step('步驟1:打開應(yīng)用'):
            print('應(yīng)用已打開')
        with allure.step('步驟2:進入登錄頁面'):
            print('登錄頁面已打開')
        with allure.step('步驟3:輸入用戶名和密碼'):
            print('用戶名和密碼輸入成功')
        print('登錄測試用例:登錄成功')

    @allure.story('登錄成功')
    def test_login_sucess2(self):
        assert '1' == 1
        print('登錄測試用例:登錄成功')

    @allure.story('登錄失敗')
    def test_login_failure_a(self):
        print('登錄測試用例:登錄失敗,用戶名缺失')

    @allure.story('登錄失敗')
    def test_login_failure_b(self):
        print('登錄測試用例:登錄失敗,密碼缺失')

    @allure.story('登錄失敗')
    def test_login_failure_c(self):
        with allure.step('輸入用戶名'):
            print('已輸入用戶名')
        with allure.step('輸入密碼'):
            print('已輸入密碼')
        with allure.step('點擊登錄'):
            print('已點擊登錄')
        print('登錄測試用例:登錄失敗,密碼錯誤')


@allure.feature('搜索模塊')
class TestSearch():
    def test_search1(self):
        print('搜索用例1')

    TEST_CASE_LINK = 'https://mirrors.huaweicloud.com/'
    @allure.testcase(TEST_CASE_LINK,'測試用例連接')
    def test_search2(self):
        print('搜索用例2')
    @allure.step('搜索步驟')
    def test_search3(self):
        print('搜索用例3')

依次執(zhí)行命令: 

  pytest test_allure.py --alluredir=./result --clean-alluredir

  allure serve ./result

chenshifengdeMacBook-Pro:testcode chenshifeng$ pytest test_allure.py --alluredir=./result --clean-alluredir
============================================================================= test session starts =============================================================================
platform darwin -- Python 3.9.0, pytest-6.1.1, py-1.9.0, pluggy-0.13.1
rootdir: /Users/chenshifeng/MyCode/PythonCode/SFDSZL/test_pytest, configfile: pytest.ini
plugins: allure-pytest-2.8.18
collected 8 items                                                                                                                                                             

test_allure.py .F......                                                                                                                                                 [100%]

================================================================================== FAILURES ===================================================================================
________________________________________________________________________ TestLogin.test_login_sucess2 _________________________________________________________________________

self = <test_allure.TestLogin object at 0x7fef3d5cba90>

    @allure.story('登錄成功')
    def test_login_sucess2(self):
>       assert '1' == 1
E       AssertionError: assert '1' == 1

test_allure.py:27: AssertionError
=========================================================================== short test summary info ===========================================================================
FAILED test_allure.py::TestLogin::test_login_sucess2 - AssertionError: assert '1' == 1
========================================================================= 1 failed, 7 passed in 0.07s =========================================================================
chenshifengdeMacBook-Pro:testcode chenshifeng$ allure serve ./result 
Generating report to temp directory...
Report successfully generated to /var/folders/p0/3_7fwrvx6n3ftpfd4wjb01300000gn/T/7024790777193223986/allure-report
Starting web server...
2020-10-13 21:39:56.174:INFO::main: Logging initialized @6818ms to org.eclipse.jetty.util.log.StdErrLog
Server started at <http://192.168.12.100:58977/>. Press <Ctrl+C> to exit

生成的報告:

allure特性-testcase

關(guān)聯(lián)測試用例(可以直接給測試用例的地址鏈接)

例子:

TEST_CASE_LINK = 'https://mirrors.huaweicloud.com/'
@allure.testcase(TEST_CASE_LINK,'測試用例連接')
def test_search(self):
    print('搜索用例')

按重要性級別進行一定范圍測試

通常測試有P0、冒煙測試、驗證上線測試。按重要性級別來執(zhí)行的,比如上線要把主流程和重要模塊都跑一遍,可通過以下方法解決

通過附加@pytest.mark標記

通過allure.feature,allure.story

也可以通過allure.severity來附加標記

  • 級別:
  • trivial:不重要,輕微缺陷(必輸項無提示,或者提示不規(guī)范)
  • minor 不太重要,次要缺陷(界面錯誤與UI需求不符)
  • normal:正常問題,普通缺陷(數(shù)值計算錯誤)
  • critical:嚴重,臨界缺陷(功能點缺失)
  • blocker:阻塞,中斷缺陷(客戶端程序無響應(yīng),無法執(zhí)行下一步操作)

使用方法:

   在方法、函數(shù)和類上面加 @allure.severity(allure.severity_level.TRIVIAL)

執(zhí)行:

   pytest -s -v 文件名 --allure-severities normal,critical

舉例說明:

#!/usr/bin/python
# -*- coding: UTF-8 -*-
"""
@author:chenshifeng
@file:test_severity.py
@time:2020/10/11
"""
import allure
import pytest


# 不加任何標記,默認normal
def test_with_no_severity():
    pass


# trivial:不重要,輕微缺陷(必輸項無提示,或者提示不規(guī)范)
@allure.severity(allure.severity_level.TRIVIAL)
def test_with_trivial_severity():
    pass


# minor 級別 不太重要,次要缺陷(界面錯誤與UI需求不符)
@allure.severity(allure.severity_level.MINOR)
def test_with_minor_severity():
    pass


# normal:正常問題,普通缺陷(數(shù)值計算錯誤)
@allure.severity(allure.severity_level.NORMAL)
def test_with_normal_severity():
    pass


# critical:嚴重,臨界缺陷(功能點缺失)
@allure.severity(allure.severity_level.CRITICAL)
def test_with_ritical_severity():
    pass


# blocker:阻塞,中斷缺陷(客戶端程序無響應(yīng),無法執(zhí)行下一步操作)
@allure.severity(allure.severity_level.BLOCKER)
def test_with_blocker_severity():
    pass


@allure.severity(allure.severity_level.NORMAL)
class TestClassWithNormalSeverity(object):

    # 不加任何標記,默認為同class級別
    def test_inside_with_normal_severity(self):
        pass

    # 重新設(shè)置了critical級別
    @allure.severity(allure.severity_level.CRITICAL)
    def test_inside_with_critical_severity(self):
        pass

執(zhí)行:

chenshifengdeMacBook-Pro:testcode chenshifeng$ pytest test_severity.py --alluredir=./result --clean-alluredir -vs
============================================================================= test session starts =============================================================================
platform darwin -- Python 3.9.0, pytest-6.1.1, py-1.9.0, pluggy-0.13.1 -- /usr/local/bin/python3.9
cachedir: .pytest_cache
rootdir: /Users/chenshifeng/MyCode/PythonCode/SFDSZL/test_pytest, configfile: pytest.ini
plugins: allure-pytest-2.8.18
collected 8 items                                                                                                                                                             

test_severity.py::test_with_no_severity PASSED
test_severity.py::test_with_trivial_severity PASSED
test_severity.py::test_with_minor_severity PASSED
test_severity.py::test_with_normal_severity PASSED
test_severity.py::test_with_ritical_severity PASSED
test_severity.py::test_with_blocker_severity PASSED
test_severity.py::TestClassWithNormalSeverity::test_inside_with_normal_severity PASSED
test_severity.py::TestClassWithNormalSeverity::test_inside_with_critical_severity PASSED

============================================================================== 8 passed in 0.03s ==============================================================================
chenshifengdeMacBook-Pro:testcode chenshifeng$ allure serve ./result 
Generating report to temp directory...
Report successfully generated to /var/folders/p0/3_7fwrvx6n3ftpfd4wjb01300000gn/T/17788207943997663035/allure-report
Starting web server...
2020-10-13 22:27:49.842:INFO::main: Logging initialized @6620ms to org.eclipse.jetty.util.log.StdErrLog
Server started at <http://192.168.12.100:59696/>. Press <Ctrl+C> to exit

終極用例:

百度搜索:

#!/usr/bin/python
# -*- coding: UTF-8 -*-
"""
@author:chenshifeng
@file:test_baidudemo.py
@time:2020/10/13
"""
import pytest
import allure
from selenium import webdriver
import time

@allure.testcase('https://www.github.com')
@allure.feature("百度搜索")
@pytest.mark.parametrize('test_data1',['allure','pytest','unittest'])
def test_steps_demo(test_data1):
    with allure.step('打開百度網(wǎng)頁'):
        driver=webdriver.Chrome()
        driver.get('http://www.baidu.com')
        driver.maximize_window()
    with allure.step(f'輸入搜索詞:{test_data1}'):
        driver.find_element_by_id('kw').send_keys(test_data1)
        time.sleep(2)
        driver.find_element_by_id('su').click()
        time.sleep(2)
    with allure.step('保存圖片'):
        driver.save_screenshot('./screenshot/baidu.png')
        allure.attach.file('./screenshot/baidu.png',attachment_type=allure.attachment_type.PNG)
    with allure.step('關(guān)閉瀏覽器'):
        driver.quit()

執(zhí)行:

chenshifengdeMacBook-Pro:testcode chenshifeng$ pytest test_baidudemo.py --alluredir=./result --clean-alluredir -vs
============================================================================= test session starts =============================================================================
platform darwin -- Python 3.9.0, pytest-6.1.1, py-1.9.0, pluggy-0.13.1 -- /usr/local/bin/python3.9
cachedir: .pytest_cache
rootdir: /Users/chenshifeng/MyCode/PythonCode/SFDSZL/test_pytest, configfile: pytest.ini
plugins: allure-pytest-2.8.18
collected 3 items                                                                                                                                                             

test_baidudemo.py::test_steps_demo[allure] PASSED
test_baidudemo.py::test_steps_demo[pytest] PASSED
test_baidudemo.py::test_steps_demo[unittest] PASSED

============================================================================= 3 passed in 24.65s ==============================================================================
chenshifengdeMacBook-Pro:testcode chenshifeng$ allure serve ./result 
Generating report to temp directory...
Report successfully generated to /var/folders/p0/3_7fwrvx6n3ftpfd4wjb01300000gn/T/18005664130273264423/allure-report
Starting web server...
2020-10-13 23:03:39.221:INFO::main: Logging initialized @7360ms to org.eclipse.jetty.util.log.StdErrLog
Server started at <http://192.168.12.100:60775/>. Press <Ctrl+C> to exit

報告:

總結(jié)

到此這篇關(guān)于python自動化之如何利用allure生成測試報告的文章就介紹到這了,更多相關(guān)python allure生成測試報告內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 跟老齊學(xué)Python之變量和參數(shù)

    跟老齊學(xué)Python之變量和參數(shù)

    對于變量和參數(shù),不管是已經(jīng)敲代碼多年的老鳥,還是剛剛接觸編程的小白,都會有時候清楚,有時候又有點模糊。因為,在實際應(yīng)用中,它們之間分分離離,比如,敲代碼都知道,x=3中x是變量,它不是參數(shù),但是在函數(shù)y=3x+4中,x是變量,也是參數(shù)。
    2014-10-10
  • Python+wxPython實現(xiàn)自動生成PPTX文檔程序

    Python+wxPython實現(xiàn)自動生成PPTX文檔程序

    這篇文章主要介紹了如何使用 wxPython 模塊和 python-pptx 模塊來編寫一個程序,用于生成包含首頁、內(nèi)容頁和感謝頁的 PPTX 文檔,感興趣的小伙伴可以學(xué)習(xí)一下
    2023-08-08
  • TensorFlow用expand_dim()來增加維度的方法

    TensorFlow用expand_dim()來增加維度的方法

    今天小編就為大家分享一篇TensorFlow用expand_dim()來增加維度的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-07-07
  • Python腳本,標識符,變量使用,腳本語句,注釋,模塊引用詳解

    Python腳本,標識符,變量使用,腳本語句,注釋,模塊引用詳解

    這篇文章主要為大家詳細介紹了Python腳本,標識符,變量使用,腳本語句,注釋,模塊引用,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-02-02
  • Python裝飾器如何實現(xiàn)修復(fù)過程解析

    Python裝飾器如何實現(xiàn)修復(fù)過程解析

    這篇文章主要介紹了Python裝飾器如何實現(xiàn)修復(fù)過程解析,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-09-09
  • python繪制雪景圖

    python繪制雪景圖

    這篇文章主要為大家詳細介紹了python繪制雪景圖,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-12-12
  • 用python打包exe應(yīng)用程序及PyInstaller安裝方式

    用python打包exe應(yīng)用程序及PyInstaller安裝方式

    PyInstaller 制作出來的執(zhí)行文件并不是跨平臺的,如果需要為不同平臺打包,就要在相應(yīng)平臺上運行PyInstaller進行打包。今天通過本文給大家介紹用python打包exe應(yīng)用程序及PyInstaller安裝方式,感興趣的朋友一起看看吧
    2021-12-12
  • python 爬取疫情數(shù)據(jù)的源碼

    python 爬取疫情數(shù)據(jù)的源碼

    這篇文章主要介紹了python 爬取疫情數(shù)據(jù),,程序源碼簡單易懂,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-02-02
  • 用Python寫一個自動木馬程序

    用Python寫一個自動木馬程序

    這篇文章主要介紹了用Python寫一個自動木馬程序的方法,本文給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下
    2019-09-09
  • Python繪制專業(yè)的K線圖 源代碼解析

    Python繪制專業(yè)的K線圖 源代碼解析

    這篇文章主要介紹了Python繪制專業(yè)的K線圖,使用Python繪制一幅專業(yè)的K線圖,是量化投資和金融數(shù)據(jù)分析的必備功課。下面我將從K線圖簡介、數(shù)據(jù)獲取、K線圖繪制及成交量繪制等方面,結(jié)合源代碼,一步步實現(xiàn)專業(yè)K線圖的繪制,需要的朋友可以參考下
    2021-10-10

最新評論

景泰县| 青神县| 明水县| 九台市| 酒泉市| 敦煌市| 长武县| 宜宾市| 迁西县| 南部县| 日喀则市| 芜湖县| 民乐县| 绥滨县| 玉林市| 邛崃市| 丽江市| 永春县| 广水市| 隆回县| 安仁县| 汤阴县| 家居| 融水| 寻甸| 汝州市| 阳高县| 桂阳县| 宣威市| 巴彦县| 岫岩| 桦川县| 巴青县| 府谷县| 大同市| 兰考县| 达孜县| 福建省| 兴安县| 高密市| 巫山县|