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

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

安裝

安裝allure

Windows用戶:

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

MAC用戶:

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

手動安裝:

  • 可以從官網?Central Repository: io/qameta/allure/allure-commandline?手動下載
  • 目前最新版本為2.13.6???

下載后解壓并配置環境變量

安裝allure-pytest插件

  • pip install allure-pytest

allure常用特性

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

步驟:

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

allure特性—feature/story

@allure.feature與@allure.store的關系

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

step特性

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

運行:

  在測試執行期間收集結果

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

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

查看測試報告:

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

      allure serve ./result

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

      生成報告:

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

      打開報告:

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

舉個例子:

有如下代碼文件

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

#!/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:打開應用'):

????????????print('應用已打開')

????????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')

依次執行命令: 

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

  allure serve ./result

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

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

關聯測試用例(可以直接給測試用例的地址鏈接)

例子:

1

2

3

4

TEST_CASE_LINK = 'https://mirrors.huaweicloud.com/'

@allure.testcase(TEST_CASE_LINK,'測試用例連接')

def test_search(self):

????print('搜索用例')

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

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

通過附加@pytest.mark標記

通過allure.feature,allure.story

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

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

使用方法:

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

執行:

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

舉例說明:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

#!/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:不重要,輕微缺陷(必輸項無提示,或者提示不規范)

@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:正常問題,普通缺陷(數值計算錯誤)

@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:阻塞,中斷缺陷(客戶端程序無響應,無法執行下一步操作)

@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

????# 重新設置了critical級別

????@allure.severity(allure.severity_level.CRITICAL)

????def test_inside_with_critical_severity(self):

????????pass

執行:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

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

終極用例:

百度搜索:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

#!/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('打開百度網頁'):

????????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('關閉瀏覽器'):

????????driver.quit()

執行:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

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

報告:

?現在我也找了很多測試的朋友,做了一個分享技術的交流群,共享了很多我們收集的技術文檔和視頻教程。
如果你不想再體驗自學時找不到資源,沒人解答問題,堅持幾天便放棄的感受
可以加入我們一起交流。而且還有很多在自動化,性能,安全,測試開發等等方面有一定建樹的技術大牛
分享他們的經驗,還會分享很多直播講座和技術沙龍
可以免費學習!劃重點!開源的!!!
qq群號:485187702【暗號:csdn11】

最后感謝每一個認真閱讀我文章的人,看著粉絲一路的上漲和關注,禮尚往來總是要有的,雖然不是什么很值錢的東西,如果你用得到的話可以直接拿走!?希望能幫助到你!【100%無套路免費領取】

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/719224.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/719224.shtml
英文地址,請注明出處:http://en.pswp.cn/news/719224.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

成功的交易沒有對錯,只有邏輯

大部分人將交易失敗歸咎于心態&#xff0c;但其實我們是否認真思考過自己的基本功是否扎實呢&#xff1f;這篇文章將引導你換個角度看待交易&#xff0c;讓你明白自己應該努力的方向。 曾經&#xff0c;你或許認為資金體量小、信息不對稱、技術不過關、心態不過硬是阻礙交易發展…

TikTok外貿系統的核心功能及其源代碼分享!

隨著全球化的不斷推進&#xff0c;外貿業務成為越來越多企業的增長動力&#xff0c;TikTok作為一個全球性的社交媒體平臺&#xff0c;其用戶基數龐大、活躍度高&#xff0c;為外貿業務提供了無限的商機。 為了幫助企業在TikTok上更好地開展外貿業務&#xff0c;TikTok外貿系統…

【DDD】學習筆記-聚合和聚合根:怎樣設計聚合?

今天我們來學習聚合&#xff08;Aggregate&#xff09;和聚合根&#xff08;AggregateRoot&#xff09;。 我們先回顧下上一講&#xff0c;在事件風暴中&#xff0c;我們會根據一些業務操作和行為找出實體&#xff08;Entity&#xff09;或值對象&#xff08;ValueObject&…

47、WEB攻防——通用漏洞Java反序列化EXP生成數據提取組件安全

文章目錄 序列化和反序列化的概念&#xff1a; 序列化&#xff1a;把java對象轉換成字節流的過程&#xff1b;反序列化&#xff1a;把字節流恢復為java對象的過程。 對象的序列化主要有兩種用途&#xff1a; 把對象的字節流永久的保存在硬盤上&#xff0c;通常存放在一個文件…

網絡分析工具簡介及匯總

文章目錄 一、網絡分析工具軟件是什么二、網絡分析工具軟件作用三、常見的網絡分析工具 一、網絡分析工具軟件是什么 網絡分析工具軟件是一種用于捕獲、分析和解釋網絡數據包的工具。它們可以直接從網絡接口上捕獲數據包&#xff0c;并提供詳細的信息和統計數據&#xff0c;以幫…

xss.haozi.me:0x0B

<svg><script>(1)</script>

洛谷 B3620 x 進制轉 10 進制

題目描述 給一個小整數 x 和一個 x 進制的數 S。將 S 轉為 10 進制數。對于超過十進制的數碼&#xff0c;用 A&#xff0c;B&#xff0c;…… 表示。 輸入格式 第一行一個整數 x; 第二行一個字符串 S。 輸出格式 輸出僅包含一個整數&#xff0c;表示答案。 輸入輸出樣例…

【JavaScript】面試手撕淺拷貝

【JavaScript】面試手撕淺拷貝 引入 淺拷貝和深拷貝應該是面試時非常常見的問題了&#xff0c;為了能將這兩者說清楚&#xff0c;于是打算用兩篇文章分別解釋下深淺拷貝。 PS: 我第一次聽到拷貝這個詞&#xff0c;有種莫名的熟悉感&#xff0c;感覺跟某個英文很相似&#xff…

Linux文本處理三劍客:awk(內置函數詳解筆記)

Linux系統中&#xff0c;AWK 是一個非常強大的文本處理工具&#xff0c;它的內置函數使得對文本數據進行處理更加高效和便捷。 本文將介紹 AWK 內置函數的幾種主要類型&#xff1a; 算數函數字符串函數時間函數位操作函數其他常用函數 我們將使用一個示例文本文件來演示這些函…

局域網如何搭建服務器?

隨著網絡的普及和應用場景的不斷拓展&#xff0c;局域網搭建服務器逐漸成為大家關注的話題。在日常生活和工作中&#xff0c;我們經常需要通過局域網和互聯網進行文件共享、資源訪問等操作&#xff0c;而搭建服務器則是實現這些功能的重要手段之一。本文將針對局域網搭建服務器…

SwiftUI 如何在運行時從底層動態獲取任何 NSObject 對象實例

概覽 眾所周知,SwiftUI 的推出極大地方便了我們這些禿頭碼農們搭建 App 界面。不過,有時我們仍然需要和底層的 UIKit 對象打交道。比如:用 SwiftUI 未暴露出對象的接口改變視圖的行為或外觀。 從上圖可以看到,我們 SwiftUI 代碼并沒有設置視圖的背景色,那么它是怎么變成綠…

vscode 本地/遠程添加python解釋器

文章目錄 1. 背景2. 增加python解釋器 1. 背景 我們在使用 vscode 去遠程調試代碼時&#xff0c;如果環境存在多個 Python 版本&#xff08;如用 conda 管理&#xff09;&#xff0c;沒有選擇正確的 Python 解釋器會導致少包、庫不適配等各種問題 2. 增加python解釋器 windo…

鴻蒙系統適配的流程

鴻蒙系統適配的流程通常涉及以下關鍵步驟&#xff0c;以下是鴻蒙系統適配的一般流程&#xff0c;具體流程可能會根據項目的具體需求和開發團隊的情況進行調整和優化。北京木奇移動技術有限公司&#xff0c;專業的軟件外包開發公司&#xff0c;歡迎交流合作。 1. 準備工作&#…

超越脆弱:用否定法策略優化考研復試準備

很多情況下,你的想象力會為當前的世界添加一些東西。很抱歉,我會在本章中告訴你,這種方法完全是落后的方法:根據脆弱性和反脆弱性的概念,嚴格來說,正確的想象就是從未來中排除或削減不屬于未來的東西,采用否定法,脆弱的事物終將破碎。——《反脆弱:從不確定性中獲益》…

盤點:國家智能算力中心

文章目錄 1. Main2. My thoughtsReference 1. Main 按照《中國算力白皮書&#xff08;2022年&#xff09;》的定義&#xff0c;算力主要分為四部分&#xff1a;通用算力、智能算力、超算算力、邊緣算力。通用算力以CPU芯片輸出的計算能力為主&#xff1b;智能算力以GPU、FPGA、…

【一起學習Arcade】(6):屬性規則實例_約束規則和驗證規則

一、約束規則 約束規則用于指定要素上允許的屬性配置和一般關系。 與計算規則不同&#xff0c;約束規則不用于填充屬性&#xff0c;而是用于確保要素滿足特定條件。 簡單理解&#xff0c;約束規則就是約束你的編輯操作在什么情況下可執行。 如果出現不符合規則的操作&#…

CGI中使用Cookie

文章目錄 CGI中使用Cookiecookie的語法設置Cookie獲取Cookie檢索Cookie信息文件上傳實例1. 創建HTML表單2. 編寫Python腳本處理上傳 文件下載對話框 CGI中使用Cookie 在CGI&#xff08;Common Gateway Interface&#xff09;中使用Cookie涉及設置和獲取由Web服務器發送到瀏覽器…

java導出多個xml文件的壓縮zip

代碼&#xff1a; // 設置響應頭response.setCharacterEncoding("UTF-8");//設置響應的字符編碼為UTF-8response.setContentType("application/octet-stream");//設置響應的內容類型為二進制流&#xff0c;通常用于文件下載。response.setHeader("Con…

解釋一下前端框架中的虛擬DOM(virtual DOM)和實際DOM(real DOM)之間的關系。

聚沙成塔每天進步一點點 ? 專欄簡介 前端入門之旅&#xff1a;探索Web開發的奇妙世界 歡迎來到前端入門之旅&#xff01;感興趣的可以訂閱本專欄哦&#xff01;這個專欄是為那些對Web開發感興趣、剛剛踏入前端領域的朋友們量身打造的。無論你是完全的新手還是有一些基礎的開發…

leetcode日記(36)全排列

想思路想了很久……思路對了應該會很好做。 我的思路是這樣的&#xff1a;只變化前n個數字&#xff0c;不斷增加n&#xff0c;由2到nums.size()&#xff0c;使用遞歸直到得到所有結果 代碼如下&#xff1a; class Solution { public:vector<vector<int>> permut…