Pytest系列(18)- 超美測試報告外掛之allure-pytest的基礎使用
阿新 • • 發佈:2020-11-25
一、官方介紹
- Allure Framework是一種靈活的輕量級多語言測試報告工具,不僅可以以簡潔的Web報告形式非常簡潔地顯示已測試的內容,也允許參與開發過程的每個人從日常測試中提取最大程度的有用資訊
- 從開發/質量保證的角度來看,Allure報告可以縮短常見缺陷的生命週期:可以將測試失敗劃分為bug和損壞的測試,還可以配置log,step,fixture,attachments,timings,歷史記錄以及與TMS的整合以及Bug跟蹤系統,因此負責任的開發人員和測試人員將掌握所有資訊
- 從管理人員的角度來看,Allure提供了一個清晰的“全域性”,涵蓋了已涵蓋的功能,缺陷聚集的位置,執行時間表的外觀以及許多其他方便的事情
- Allure的模組化和可擴充套件性確保您始終能夠微調某些東西,以使Allure更適合您
二、個人介紹
- 對於管理層來說,測試報告當然是越直觀、簡潔、資料清晰越好,而Allure就滿足以上這麼多點,而且很好的和pytest集成了
- 相比於pytest-html來說,Allure的報告真的是十全十美鴨!!
- 唯一不足的就是,拓展功能需要在測試用例集上加裝飾器
三、安裝外掛
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 │
4.1 最外層的conftest.py
# 外層conftest.py
@pytest.fixture(scope="session")
def login():
print("====登入功能,返回賬號,token===")
name = "testyy"
token = "npoi213bn4"
yield name, token
print("====退出登入!!!====")
4.2 最外層的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}")
4.3 test_51job包下的conftest.py
import pytest
@pytest.fixture(scope="module")
def open_51(login):
name, token = login
print(f"###使用者 {name} 開啟51job網站###")
4.4 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)
4.5 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)
4.6 test_weibo包下的conftest.py
import pytest
@pytest.fixture(scope="function")
def open_weibo(login):
name, token = login
print(f"&&& 使用者 {name} 返回微博首頁 &&&")
4.7 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)
執行命令
要使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來分組測試用例了