Pytest系列(18)- 超美測試報告插件之allure-pytest的基礎使用

如果你還想從頭學起Pytest,可以看看這個系列的文章哦!

//www.cnblogs.com/poloyy/category/1690628.html

 

官方介紹

  1. Allure Framework是一種靈活的輕量級多語言測試報告工具,不僅可以以簡潔的Web報告形式非常簡潔地顯示已測試的內容,也允許參與開發過程的每個人從日常測試中提取最大程度的有用資訊
  2. 從開發/品質保證的角度來看,Allure報告可以縮短常見缺陷的生命周期:可以將測試失敗劃分為bug和損壞的測試,還可以配置log,step,fixture,attachments,timings,歷史記錄以及與TMS的集成以及Bug跟蹤系統,因此負責任的開發人員和測試人員將掌握所有資訊
  3. 從管理人員的角度來看,Allure提供了一個清晰的「全局」,涵蓋了已涵蓋的功能,缺陷聚集的位置,執行時間表的外觀以及許多其他方便的事情
  4. Allure的模組化和可擴展性確保您始終能夠微調某些東西,以使Allure更適合您

 

個人介紹

  1. 對於管理層來說,測試報告當然是越直觀、簡潔、數據清晰越好,而Allure就滿足以上這麼多點,而且很好的和pytest集成了
  2. 相比於pytest-html來說,Allure的報告真的是十全十美鴨!!
  3. 唯一不足的就是,拓展功能需要在測試用例集上加裝飾器

 

安裝插件

pip3 install allure-pytest -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com

 

快速入門

這是運行程式碼的包結構

# 是項目文件夾名稱
15allure
│  conftest.py
│  test_1.py
│  __init__.py
│              
├─test_51job
│  │  conftest.py
│  │  test_case1.py
│  │  __init__.py 
│          
├─test_toutiao
│  │  test_case2.py
│
├─test_weibo
│  │  conftest.py
│  │  test_case3.py
│  │  __init__.py 
│  

 

最外層的conftest.py

# 外層conftest.py

@pytest.fixture(scope="session")
def login():
    print("====登錄功能,返回帳號,token===")
    name = "testyy"
    token = "npoi213bn4"
    yield name, token
    print("====退出登錄!!!====")

View Code

 

最外層的test_1.py

import pytest


@pytest.mark.parametrize("n", list(range(5)))
def test_get_info(login, n):
    sleep(1)
    name, token = login
    print("***基礎用例:獲取用戶個人資訊***", n)
    print(f"用戶名:{name}, token:{token}")

View Code

 

test_51job包下的conftest.py

import pytest


@pytest.fixture(scope="module")
def open_51(login):
    name, token = login
    print(f"###用戶 {name} 打開51job網站###")

View Code

 

test_51job包下的test_case1.py

from time import sleep

import pytest


@pytest.mark.parametrize("n", list(range(5)))
def test_case2_01(open_51, n):
    sleep(1)
    print("51job,列出所有職位用例", n)


@pytest.mark.parametrize("n", list(range(5)))
def test_case2_02(open_51, n):
    sleep(1)
    print("51job,找出所有python崗位", n)

View Code

 

test_toutiao包下的test_case2.py

from time import sleep

import pytest


@pytest.mark.parametrize("n", list(range(5)))
def test_no_fixture(login, n):
    sleep(1)
    print("==沒有__init__測試用例,我進入頭條了==", login)

View Code

 

test_weibo包下的conftest.py

import pytest


@pytest.fixture(scope="function")
def open_weibo(login):
    name, token = login
    print(f"&&& 用戶 {name} 返回微博首頁 &&&")

View Code

 

test_weibo包下的test_case3.py

from time import sleep

import pytest


@pytest.mark.parametrize("n", list(range(5)))
class TestWeibo:
    def test_case1_01(self, open_weibo, n):
        sleep(1)
        print("查看微博熱搜", n)

    def test_case1_02(self, open_weibo, n):
        sleep(1)
        print("查看微博范冰冰", n)

View Code

 

執行命令

要使Allure能夠在測試執行期間收集測試結果,只需添加 –alluredir 選項,並提供指嚮應存儲結果的文件夾的路徑

pytest -n auto --alluredir=allure

 

生成出來的結果

可以看到,這不是我們想要的結果,一堆json、txt文件….

 

要在測試完成後查看實際報告,需要使用Allure命令行來讓測試結果生成報告

allure serve allure

然後就會自動在默認瀏覽器中顯示生成的報告

 

查看suites(函數級別的測試用例)

從包名-模組名-測試用例

 

查看suites(類級別的測試用例)

從包名-模組名-類名-測試用例

 

查看測試用例詳情

  • parameters:如果用了 @pytest.mark.parametrize ,在右側的parameters是可以看到傳了什麼參數和對應的值
  • set up:調用fixture的前置操作
  • tear down:調用fixture的後置操作 

 

Allure報告結構

  • Overview:總覽
  • Categories:類別,默認是分了failed和error,凡是執行結果是其中一個的都會被歸到類裡面,可以通過這裡快捷查看哪些用例是failed和error的
  • Suites:測試套件,就是所有用例的層級關係,可以根據package、module、類、方法來查找用例
  • Graphs:測試結果圖形化,包括用例執行結果的分布圖,優先順序,耗時等
  • Timeline:可以看到測試用例精確的測試時序(執行順序),包括執行時間
  • Behaviors:行為驅動,根據epic、feature、story來分組測試用例(後面會講到)
  • Packages:這就是按照package、module來分組測試用例了