在使用nose自帶的xunit生成xml文件生成測試報告后,領導說報告不夠炫,沒有百分比效果,且在web自動化時的截圖不美觀,html很多情況下沒有顯示圖片(nose框架截圖方法這里),正好,allure_report報告框架滿足所有要求。
一、介紹
這里可以查看官方信息,http://allure.qatools.ru/,報告還支持中文
還有前輩寫的實踐之路:https://testerhome.com/topics/5738
二、基於nose框架
從官網介紹中,我們可以得知,allure支持python nose框架
1) 安裝:
nose下allure的插件官網:https://pypi.python.org/pypi/nose-allure-plugin
這里我們使用pip安裝,安裝過程中會自動安裝pytest、lxml、six、eunum34等插件
C:\Users\Administrator>pip install nose-allure-plugin
目前只支持python2的版本,python3的同學請繞行
如果在安裝過程中出現以下問題:
Building lxml version 3.8.0. Building without Cython. ERROR: 'xslt-config' 不是內部或外部命令,也不是可運行的程序 或批處理文件。 ...... Could not find function xmlCheckVersion in library libxml2. Is libxml2 insta lled? **************************************************************************** ***** error: command 'C:\\Users\\Administrator\\AppData\\Local\\Programs\\Common\\ Microsoft\\Visual C++ for Python\\9.0\\VC\\Bin\\cl.exe' failed with exit status 2
可以指定lxml版本安裝:
pip install lxml==3.6.0
然后再次安裝
pip install nose-allure-plugin
安裝完成后,可以輸入nosetests -h,可能看到以下幾個參數,表示安裝成功
--logdir=LOGDIR --not-clear-logdir --feature=FEATURE --story=STORY --issue=ISSUE --severity=SEVERITY
2)使用
根據官網的例子熟悉下
import nose class TestBar(object): @nose.allure.severity(nose.allure.severity_level.CRITICAL) def test_bar(self): pass # custom severity @nose.allure.severity("hard") def test_bar(self): pass
運行:
nosetests -v -s allure_learn.py --with-allure --logdir=tmp --severity="critical, hard"
這里的--logdir是產生xml文件的路徑,如果是在windows下請更改格式,運行完成后,在--logdir下會產生對應的xml文件
3)在web自動化下生成截圖的方法
這點是allure report最方便的地方,不用指定文件名自動生成截圖
看以下腳本
import nose,sys from selenium import webdriver from allure.constants import AttachmentType class TestBar(): @classmethod def setUpClass(cls): cls.driver = webdriver.Firefox() cls.driver.get("http://www.baidu.com") @nose.allure.severity(nose.allure.severity_level.CRITICAL) def test_bar(self): assert 1==2
nose.allure.attach('screenshot',self.driver.get_screenshot_as_png(), type= AttachmentType.PNG) # custom severity @nose.allure.severity("hard") def test_bar_xx(self): assert 4==4 #nose.allure.attach('screenshot', self.driver.get_screenshot_as_png(), type='png') -----該方法錯誤 nose.allure.attach('screenshot', self.driver.get_screenshot_as_png(), type=AttachmentType.PNG)
運行后,我們可以看到在對應的目錄下生成了報xml報告且還有截圖文件
但這個代碼不實用,我們在實際項目中,肯定是運行測試失敗后才截圖,成功就不截圖
這里,使用tearDown方法,並判斷用例成功與否,失敗則截圖,
def tearDown(self): if sys.exc_info()[0]: nose.allure.attach('screenshot',self.driver.get_screenshot_as_png(), type= AttachmentType.PNG)
4)集成進jenkins生成report
jenkins怎么安裝,網管上也有說明,http://wiki.qatools.ru/display/AL/Allure+Jenkins+Plugin,這里不介紹
博主在linux下安裝時,發現From Maven Central一項是空是,沒有版本可以選擇,查看后發現,是安裝插件時,在jenkins updates文件夾下沒有生成對應的文件,手工添加解決
在$jenkins_home/updates查看是否存在文件ru.yandex.qatools.allure.jenkins.tools.AllureCommandlineInstaller,如果不存在,手工創建,並輸入以下內容
{"list":[{"id":"2.2.1","name":"2.2.1","url":"https://dl.bintray.com/qameta/generic/io/qameta/allure/allure/2.2.1/allure-2.2.1.zip"},{"id":"2.2.0","name":"2.2.0","url":"https://dl.bintray.com/qameta/generic/io/qameta/allure/allure/2.2.0/allure-2.2.0.zip"},{"id":"2.1.1","name":"2.1.1","url":"https://dl.bintray.com/qameta/generic/io/qameta/allure/allure/2.1.1/allure-2.1.1.zip"},{"id":"2.1.0","name":"2.1.0","url":"https://dl.bintray.com/qameta/generic/io/qameta/allure/allure/2.1.0/allure-2.1.0.zip"},{"id":"2.0.1","name":"2.0.1","url":"https://dl.bintray.com/qameta/generic/io/qameta/allure/allure/2.0.1/allure-2.0.1.zip"},{"id":"2.0.0","name":"2.0.0","url":"https://dl.bintray.com/qameta/generic/io/qameta/allure/allure/2.0.0/allure-2.0.0.zip"},{"id":"1.5.4","name":"1.5.4","url":"https://oss.sonatype.org/content/repositories/releases/ru/yandex/qatools/allure/allure-commandline/1.5.4/allure-commandline-1.5.4-standalone.zip"},{"id":"1.5.3","name":"1.5.3","url":"https://oss.sonatype.org/content/repositories/releases/ru/yandex/qatools/allure/allure-commandline/1.5.3/allure-commandline-1.5.3-standalone.zip"},{"id":"1.5.2","name":"1.5.2","url":"https://oss.sonatype.org/content/repositories/releases/ru/yandex/qatools/allure/allure-commandline/1.5.2/allure-commandline-1.5.2-standalone.zip"},{"id":"1.5.1","name":"1.5.1","url":"https://oss.sonatype.org/content/repositories/releases/ru/yandex/qatools/allure/allure-commandline/1.5.1/allure-commandline-1.5.1-standalone.zip"},{"id":"1.5.0","name":"1.5.0","url":"https://oss.sonatype.org/content/repositories/releases/ru/yandex/qatools/allure/allure-commandline/1.5.0/allure-commandline-1.5.0-standalone.zip"},{"id":"1.4.23.HOTFIX1","name":"1.4.23.HOTFIX1","url":"https://oss.sonatype.org/content/repositories/releases/ru/yandex/qatools/allure/allure-commandline/1.4.23.HOTFIX1/allure-commandline-1.4.23.HOTFIX1-standalone.zip"},{"id":"1.4.23","name":"1.4.23","url":"https://oss.sonatype.org/content/repositories/releases/ru/yandex/qatools/allure/allure-commandline/1.4.23/allure-commandline-1.4.23-standalone.zip"},{"id":"1.4.22","name":"1.4.22","url":"https://oss.sonatype.org/content/repositories/releases/ru/yandex/qatools/allure/allure-commandline/1.4.22/allure-commandline-1.4.22-standalone.zip"},{"id":"1.4.21","name":"1.4.21","url":"https://oss.sonatype.org/content/repositories/releases/ru/yandex/qatools/allure/allure-commandline/1.4.21/allure-commandline-1.4.21-standalone.zip"},{"id":"1.4.20","name":"1.4.20","url":"https://oss.sonatype.org/content/repositories/releases/ru/yandex/qatools/allure/allure-commandline/1.4.20/allure-commandline-1.4.20-standalone.zip"},{"id":"1.4.19","name":"1.4.19","url":"https://oss.sonatype.org/content/repositories/releases/ru/yandex/qatools/allure/allure-commandline/1.4.19/allure-commandline-1.4.19-standalone.zip"},{"id":"1.4.18","name":"1.4.18","url":"https://oss.sonatype.org/content/repositories/releases/ru/yandex/qatools/allure/allure-commandline/1.4.18/allure-commandline-1.4.18-standalone.zip"},{"id":"1.4.17","name":"1.4.17","url":"https://oss.sonatype.org/content/repositories/releases/ru/yandex/qatools/allure/allure-commandline/1.4.17/allure-commandline-1.4.17.zip"}]}
再次查看,From Maven Central中有版本選擇了
構建后說明:
這里的path是之前使用nosetests 生成xml時對應的文件夾,但文件夾又是基於job的工作空間的相對目錄,寫絕對目錄是不行的
5)查看結果
生成完成后,可以產生這樣的效果圖:
點擊查看:
在對應失敗的用例下,有截圖顯示,
更新一下:
有同學在使用時,不想每次用jenkins來生成數據來查看報告,可以自行下載allure commandline工具,用以下方式來生成報告:
allure generate directory-with-results/ -o directory-with-report
生成完成后,點擊index.html文件,發現數據未顯示出來
原來少最做一步,要開啟服務才可以查看,命令如下:
allure report open
如果還覺得麻煩的,還要在運行完測試用例后直接輸入以下命令:
allure serve directory-results
其中dirceotry-results是你生成xml的路徑
---------------------11.16更新------------------------
在實際使用時,allure在執行時會將report生成的截圖和xml刪除,有時候分步驟使用時,會是一個麻煩事,
所以在nose使用時,可以使用參數:
--not-clear-logdir
來避免清空文件夾,如下:
nosetests --with-allure --logdir=results --not-clear-logdir
然后在windows下時,運行完幾個測試后,還需手工清除截圖及xml,這里記錄下bat使用:
del /s /Q allure-results
----表示:只刪除文件而不能刪除子文件夾
---------------------2018.1.9更新------------------------
又來更新了,allure在首頁界面有個環境信息,本來我覺得用處不大,但是TMD運營又提需求,多環境同時跑時,最后出的報告要看出是什么環境跑的,正好allure正好有該功能,很簡單,只要在代碼中加入下面一行:
nose.allure.environment(URL='http://192.168.1.1', Username=u'huzhq',project=u'afdffd')
生成的報告就有該信息