天天看點

pytest+allure架構搭建(MacOS)

        Allure架構是一種靈活的輕量級多語言測試報告工具,它不僅能夠以簡潔的web報告形式顯示已測試的内容,而且允許參與開發過程的每個人從測試的日常執行中提取最大限度的有用資訊。具有開源,輕量級,多語言支援,而且支援主流架構內建testng、junit、支援jenkins內建,具有強大的注解功能,同時支援測試報告定制等優點。關于Allure的更多介紹可以檢視官網:http://allure.qatools.ru/

        Allure是基于Java的一個程式,需要jdk1.8及以上的環境,是以需要安裝jdk1.8及以上,jdk的安裝網上有較多教程在此不再贅述,具體參考網上教程。

        pytest架構安裝參見文章https://blog.csdn.net/dou_being/article/details/102460803。

        下面進入正題,開始介紹allure架構的安裝過程:

  • 安裝allure-pytest:pip3 install allure-pytest

注意:pytest-allure-adaptor不建議安裝,因為pytest-allure-adaptor庫基本被python3放棄了,運作不是很友好,運作時還會報錯,如果已安裝且運作有報錯,可以通過:pip3 uninstall pytest-allure-adaptor進行解除安裝規避。

  • 安裝allure2

(1)在https://github.com/allure-framework/allure2/releases,下載下傳後解壓到運作的pytest的目錄,比如我的pytest運作目錄:/Users/使用者名/env_pytest/,如圖

pytest+allure架構搭建(MacOS)

(2)打開bin目錄,執行allure可執行檔案(Windows則點選allure.bat檔案),如圖

pytest+allure架構搭建(MacOS)
pytest+allure架構搭建(MacOS)

(3)将allure/allure.bat檔案設定為系統環境變量path下,這樣在終端任意目錄都可以執行:

  1. 在終端輸入:open ~/.bash_profile,然後輸入allure的bin目錄路徑,如圖
    pytest+allure架構搭建(MacOS)
  2. 然後輸入source ~/.bash_profile,使配置立即生效
  3. 在終端輸入allure,列印出一串資訊即配置成功,如圖
    pytest+allure架構搭建(MacOS)
  4. 檢視allure的版本号:allure --version,如圖
    pytest+allure架構搭建(MacOS)
  • 生成xml測試報告:
pytest -s -q --alluredir report
           

或者指定路徑生成xml測試報告:

pytest -s -q --alluredir /Users/使用者/Downloads/report 
           

比如:pytest -s -q --alluredir /Users/使用者/Desktop/pytestDemo/report /Users/使用者/Desktop/pytestDemo/test_parametrize.py

說明:此步驟如果直接輸入pytest -s -q --alluredir report會報錯,因為沒有指定相應的測試用例路徑,上面的test_parametrize.py是我本地的一個測試用例,測試用例可以參考如下:

import pytest
import allure

"""
1.使用pytest.mark.parametrize 裝飾器實作測試用例參數化。
2.實作檢查一定的輸入和期望輸出的測試功能
"""


@allure.feature('pytest結合allure測試Demo') # feature定義被測試的功能
@pytest.mark.parametrize("test_input, expected", [("3+5", 8), ("2+4", 6), ("6*9", 42), ])
def test_eval(test_input, expected):
    assert eval(test_input) == expected


if __name__ == "__main__":
    pytest.main(['-s', '-q', '--alluredir', './report/html'])
           
  • 運作結果
    pytest+allure架構搭建(MacOS)
  • 運作allure2:使用pytest -s -q --alluredir report已經在report目錄下生成了不美觀的測試報告,如圖
    pytest+allure架構搭建(MacOS)
  • 當然xml格式的報告不夠直覺,我們需要通過allure将它轉成HTML格式的報告,執行指令:
    allure generate directory-with-results/ -o directory-with-report
               
    比如:allure generate --clean /Users/使用者/Desktop/pytestDemo/report -o /Users/使用者/Desktop/pytestDemo/report/html,如圖
    pytest+allure架構搭建(MacOS)
  • 回到pycharm,找到report目錄,裡面有個html檔案夾,然後點選index.html,選擇打開方式:Open in Browser在浏覽器中打開,如圖
    pytest+allure架構搭建(MacOS)
  • 定制測試報告,可根據具體實際情況選擇
  1. feature:标注主要功能子產品
  2. story:标注features功能子產品下的分支功能
  3. severity:标注測試用例的重要級别
  4. step:标注測試用例的重要步驟
  5. issue和testCase:标注issue、case,可以加入URL

        說明:

  1. directory-with-results 是 alluredir 生成的 xml 目錄
  2. directory-with-report 是最終生成 html 的目錄
  3. allure/allure.bat 已經加到環境變量了,是以可以用相對路徑去生成 html報告:allure generate report/ -o  report/html
  4. --clean參數用來清空已有的報告,避免覆寫時出錯     

        注意:直接用chrome浏覽器打開報告,報告會是空白頁面,解決辦法,在pycharm中右擊index.html選擇打開方式Open in Browser就可以了 。

繼續閱讀