環境前置提示:allure是基於Java的一個程序,需要Java1.8的環境,沒有安裝需要去安裝一下。
如果在cmd中能輸入java,獲取到命令信息則不管,否則需要配置系統變量:
路徑:計算機>屬性>高級>環境變量
在系統變量添加
JAVA_HOME D:\Software\JDK8(改為自己的實際路徑)
在Path中追加(沒有則新建)
%JAVA_HOME%\bin;%JAVA_HOME%\jre\bin;
1、安裝allure
alluer官網地址:http://allure.qatools.ru/
當前最新版本(allure-commandline-2.13.1.zip)下載地址:
https://github.com/allure-framework/allure2/releases/tag/2.13.1
下載解壓后,把解壓目錄放到自定義存放路徑,然后在系統變量(參見上面java設置)Path中追加:
~\放置目錄\allure-commandline-2.13.1\allure-2.13.1\bin;
點擊確定,保存。此時可以通過cmd使用allure命令,則安裝配置正確。
2、安裝allure-pytest
pip install allure-pytest
注意:如果環境裝有多個python版本,需切換到pycharm當前使用的python下面進行安裝。
使用命令 pip list 確認插件是否安裝成功
最終環境清單:
- windows7 x64
- python3.7
- pycharm-professional-2019.3.1
- pytest 5.3.2
- allure-pytest 2.8.6
- allure-commandline-2.13.1
- java1.8
3、一個簡單的用例test_simpe.py
import pytest import allure @allure.feature("測試Dome") class TestClass: @allure.story("測試用例 1") def test_one(self): x = "hello" assert 'h' in x @allure.story("測試用例 2") def test_two(self): x = "test" assert hasattr(x, 'check')
4、在pycharm底部打開terminal
其中輸入命令生成結果,命令格式:
pytest <測試目錄> --alluredir <測試結果存放目錄>
比如,我的文件夾目錄如下
所以命令為:
pytest testcase --alluredir report/allure_raw
allure收集pytest運行后產出的結果放在 reportallure_raw 文件夾中
注意:這里的 allure_raw 文件夾只存放的是測試運行結果,還不是報告!報告還需要調用 allure 命令去生成。
當前結果是像這樣的:
5、用allure美化報告
allure generate <allure測試結果目錄>-o <運行結果的目錄> <存放報告的目錄> --clean
這里命令如下:
allure generate report/allure_raw -o report/allure_report --clean
通過上面的命令運行后,就會從 allure_raw 目錄中將 pytest 運行的結果生成一個漂亮的報告,存放在 allure_report 中。
6、查看報告
在 pycharm 中可以選擇index.html通過右鍵[ Open in Browser]就可以看到報告了
這里選擇chrome瀏覽器打開,展示效果如下
注意:這里直接找到存放結果allure_report下的index.html打開,是看不到報告數據的
至此,漂亮報告是不是讓人賞心悅目,陡然感覺高尚上起來了。
問題1:如果你不是用 Pycharm 的話,可以通過 allure 命令生成服務查看
allure open 報告路徑
如:allure open D:\PYTEST eport\allure_report
將自動使用當前默認瀏覽器展示報告
問題2:過程中如果遇到AttributeError: module 'allure' has no attribute 'severity_level'問題
pip uninstall pytest-allure-adaptor
pip install allure-pytest
因為pytest-allure-adaptor和allure-pytest不能同時使用,需要卸載掉pytest-allure-adaptor。
另一種生成allure報告的方法
運行用例
cd到test_allure_demo.py所在的目錄文件,命令行執行
pytest --alluredir ./report/allure_raw
D:\soft\code\xuexipytest>pytest --alluredir ./report/allure_raw ============================= test session starts ============================= platform win32 -- Python 3.6.0, pytest-4.5.0, py-1.5.4, pluggy-0.13.1 rootdir: D:\soft\code\xuexipytest plugins: allure-pytest-2.8.6, forked-0.2, html-1.19.0, metadata-1.7.0, repeat-0.7.0, xdist-1.23.2 collected 9 items case\test_allure_demo.py .. [ 22%] case\test_x.py ...... [ 88%] case\test_y.py . [100%] ============================== 9 passed in 0.21s ==============================
執行完成后,在當前目錄下,report目錄會生成一個allure_raw的原始文件,這個只是測試報告的原始文件,不能打開成html的報告
打開html的報告需要啟動allure服務,啟動命令如下
allure serve report/allure_raw
啟動服務,它會自動給個端口,直接用默認瀏覽器打開了
D:\soft\code\xuexipytest>allure serve report/allure_raw Generating report to temp directory... Report successfully generated to C:\Users\dell\AppData\Local\Temp\6056757827461248074\allure-report Starting web server... 2019-12-08 00:41:09.921:INFO::main: Logging initialized @2228ms to org.eclipse.jetty.util.log.StdErrLog Server started at <http://192.168.1.125:35346/>. Press <Ctrl+C> to exit
查看報告
瀏覽器上打開的報告內容
點 EN 按鈕可以查看中文報告
打開測試套件,可以查看報告的詳情,顯示的還是很詳細的
內容整合來源:
http://www.51ste.com/share/det-862-1.html
https://www.cnblogs.com/yoyoketang/p/12004145.html