Python + Allure(報告)+ Jenkins(持續集成)接口自動化測試環境搭建

(這個是內部分享文檔,側重於測試報告的生成以及持續集成,至於接口框架的內部組織可以按照自己的想法來實現,有問題歡迎留言)

1.環境:
• 操作系統:windows 64位
• 軟件環境:python3.4.4;jdk1.8;jenkins1.65

2.基本環境配置:
• python 添加到系統變量
• jdk環境配置(注意,jdk安裝路徑中不要包含括號、空號,否則執行allure命令時會報 此時不應有 \java\jdk1.8.0_111 錯誤,原因是allure的bat文件解析java home的時存沒有處理)
• jenkins安裝

3.第三方庫安裝
• pytest安裝:在cmd命令下使用 pip install pytest ;
測試:在python 語言環境下import pytest 如果未報錯表示安裝成功

• pytest_allure_adaptor安裝:在cmd命令下使用 pip install pytest_allure_adaptor
安裝pytest_allure_adaptor注意事項
pytest_allure_adaptor依賴一些python的第三方庫,其中lxml庫在安裝過程中容易出現 error: Microsoft Visual C++ 10.0 is required (Unable to find vcvarsall.bat).導致整個安裝失敗。
原因:是python安裝編譯會調用本版本所用編譯器進行編譯。所以關鍵是找到正確的編譯器以及commontools路徑。2.7版本使用VS9編譯,對於3.4版本,是使用VS10編譯的.

解決辦法:
1.在cmd命令下執行 pip install wheel
2.在http://www.lfd.uci.edu/~gohlke/pythonlibs/#lxml 下載對應的,一定要是和python版本+系統版本對應的的lxml*.whl文件(如:lxml-3.4.4-cp34-none-win_amd64.whl 注意:3.4.4表示lxml版本;cp表示python版本;amd64表示系統環境64位)
3.pip install lxml*.whl

• 其他庫的安裝:都可以使用pip install 庫名稱

• 下載allure執行工具:https://github.com/allure-framework/allure-core/releases/tag/allure-core-1.4.23 ;可以將allure執行工具的bin目錄添加到系統變量path中。

4.生成allure所需要的測試結果文件
• 在cmd命令執行測試代碼:py.test 測試用例目錄 –alluredir=./allure-result
生成allure所需要的測試結果文件
py.test 測試用例目錄 –alluredir=./allure-result
其中 –alluredir 表示生成測試結果保存路徑; ./allure-result 表示當前路徑下的allure-result文件下;在生成報告前會將報告存放路徑下的文件夾清空

• 使用allure執行工具生成報告
使用allure工具生成報告
在allure執行工具的bin目錄下對於linux與windows都有對應的腳本支持。
使用方法:
前提:執行測試後生成了對應xml結果文件,如 allure-result
生成測試報告:在cmd命令行中使用 allure generate allure-result 執行後會在該目錄下生成allure-report文件,再使用allure report open 可以在本地查看報告.(備註:allure的報告需要有Server服務器apache、tomcat、jetty等才能被查看)
(在cmd命令下使用 allure help 可以查看詳細的參數說明)

5.用例中使用allure(需要 import allure):
• 在報告中增加步驟顯示,如下:使用 with pytest.allure.step(‘描述’): or with allure.step(‘描述’):
在報告中增加步驟顯示

    @pytest.allure.testcase("http://www.qq.com")
    def test_steps_demo(self):
        with pytest.allure.step('step one'):
            driver = webdriver.Chrome()
            driver.get('http://www.baidu.com')
        with pytest.allure.step('step two'):
            driver.find_element_by_id('kw').send_keys('hello')
            driver.find_element_by_id('su').click()
            time.sleep(5)
        with pytest.allure.step('step three'):
            driver.save_screenshot("b.png")
            f = open('./b.png','rb').read()
            allure.attach('this is a img',f,allure.attach_type.PNG)
        with pytest.allure.step('step four'):
            driver.quit()

• 在報告中增加額外的信息:allure.attach(’arg1’,’arg2’,’arg3’):
arg1:是在報告中顯示的名稱
arg2:表示添加的內容
arg3:表示添加的類型(支持類型:HTML,JPG,PNG,JSON,OTHER,TEXTXML)
向報告中添加額外信息
allure.attach(‘this is a attach’,’llllllll llllll aaaaa’)
往報告中添加圖片
f = open(‘./b.png’,’rb’).read()
allure.attach(‘this is a img’,f,allure.attach_type.PNG)

• 添加環境信息(注:建議放到一個用例中)
添加環境信息
allure.environment(report=’Allure report’,browser=’chrome’,version=’JiuYiBao2.0.6’)

• 利用Feature 和 Story 組織用例,如:一個大模塊M 下包含幾個小模塊A、B、C ;可以將M 設置爲Feature,A、B、C 設置爲不同的Story
添加Featrue和Story

@allure.feature('Feature1')
@allure.story('Story1')
def test_mminor():
    assert Fals

6.整合jenkins
• jenkins 分別安裝插件 Allure Jenkins Plugin 、HTML Publisher plugin
• 在系統設置中將JDK / Maven 等其他一些基本配置(建議jdk版本1.8)
• 在jenkins中添加allure執行工具

jenkins添加allure執行工具
1.先下上面提到的allure執行的壓縮文件(allure-commandline.zip)
2.在jenkins的系統配置—–Allure Commandline —-去掉自動安裝勾選框—-填寫name及剛下載的文件夾的根目錄路徑
3.點擊保存

• 新建job,在job中添加步驟 Allure report(Results:第一行表示xml文件的路徑;第二行表示生成報告的路徑)
目前做法
目前job的整個流程:
1.構建時,從svn上拉取最新的測試代碼
2.執行測試腳本並且生成allure需要xml結果文件
3.通過Allure report生成測試報告

4.設置問題追蹤
(在Allure Report 下選擇增加:Key: allure.issues.tracker.pattern Value: http://tracker.company.com/%s)

• 由於jenkins1.5.2以上版本對插件的安全性做了限制必須要在jenkins —統管理–腳本命令行中執行代碼
執行權限代碼
System.setProperty(“hudson.model.DirectoryBrowserSupport.CSP”, “default-src ‘self’; script-src ‘self’ ‘unsafe-inline’ ‘unsafe-eval’; style-src ‘self’ ‘unsafe-inline’;”)

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章