【安裝】
1.因為allure2需要在java的環境下,並且要求必須是jdk1.8級以上,所以要首先保證這一點
2.安裝pytest:
pip install pytest
3.安裝allure-pytest
:pip install allure-pytest
4.安裝allure:
下載allure2:https://github.com/allure-framework/allure2/releases,我們下載的是allure-2.8.0版本;下載后解壓,放在某個位置(建議放在C:\python35\Lib\site-packages下);配置環境變量:環境變量path中加上解壓好的文件夾下的bin目錄下的allure.bat文件的路徑(這里是:E:\python\Lib\site-packages\allure-2.8.0\bin)
【使用】
1 #coding=GBK 2 3 import pytest 4 import allure 5 from test_api.run.run import Test_run 6 from test_api.data.get_cookies_value import get_cookies_value 7 import warnings 8 import os 9 10 @allure.feature('test-api') # feature定義功能 11 class Test_Run(): 12 def setup_method(self, method): #每個模塊開始之前都會執行 13 print('start.....') 14 15 @allure.story('登錄/獲取報文') # story定義用戶場景 16 def test_1(self): 17 warnings.simplefilter("ignore", ResourceWarning) 18 # get_cookies_value() 19 a=Test_run() 20 a.run() 21 #斷言 22 assert True 23 24 def teardown_method(self, method): ##每個模塊結束之后都會執行 25 print('end.....') 26 27 if __name__ == '__main__': 28 # 生成配置信息 "-s 代表可以將執行成功的案例日志打印出來 ; -q+文件執行路徑 代表只需要執行的文件" 29 pytest.main(['-s','-q',r'E:\python_project\request_test\pytest_allure\test_api_by_pytest.py','--alluredir','./report/xml']) 30 #os模塊運行allure命令,來生成html格式的報告(根據剛剛生成的配置信息) 31 os.system("E:/python37/Lib/site-packages/allure-2.8.0/bin/allure.bat " 32 "generate " 33 "E:/python_project/request_test/pytest_allure/report/xml " 34 "-o " 35 "E:/python_project/request_test/pytest_allure/report/html")
(pytest的setup和teardown,見https://www.jianshu.com/p/a57b5967f08b)
Or:命令行執行
生成的報告如下: