Python接口測試之報告(十五)


        在本文章中,主要使用jenkins和編寫的自動化測試代碼,來生成漂亮的測試報告,關於什么是CI這些

我就不詳細的介紹了,這里我們主要是實戰為主。

       首先搭建java的環境,這個這里不做介紹。搭建好java的環境后,在https://jenkins.io/上下載jenkins,以及

https://jenkins.io/上下載tomcat,都下載完成后,進行解壓,把jenkins.war放在tomcat的webapps的目錄下,

在tomcat的bin目錄下,點擊start.bat啟動tomcat,啟動后,在瀏覽器中訪問http://localhost:8080/jenkins,首次

使用會看到需要輸入密碼,在windows環境下,密碼是在C:\Users\Administrator\.jenkins\secrets目錄下,打開

initialAdminPassword文件,復制該內容,把它copy到jenkins的密碼輸入,然后下來選擇安裝插件,關於插件

這里就不詳細的說明了。

     這里我們使用allure來生成測試報告,我們知道,在python的自動化測試中,常用生成測試報告使用的是

HTMLTestRunner庫,但是該庫生成的測試報告不是很漂亮,我們使用allure可以生成很直觀的測試報告而且

測試報告比較渲,下面就來詳細的說明如何使用它來生成很酷的測試報告步驟。

   首先需要在jenkins中安裝插件Allure Jenkins Plugin,在jenkins的插件管理中,直接搜索allure可以搜索到,然后

選擇安裝就可以了。安裝該插件成功后,點擊jenkins的“系統管理”,在系統管理中,點擊Global Tool Configuration

Global Tool Configuration的界面安裝allure,見截圖:

點擊“Allure Commandline安裝...”,會出現如下的界面,見截圖:

選擇好后,點擊save保存成功。

    下來我們需要安裝pytest,安裝的命令為:

                                                         pip install pytest
                                                         pip install pytest-allure-adaptor

在線安裝成功后,在cmd的命令中輸入pytest,如果顯示信息如下,表示安裝OK,見截圖:

     OK,下面我們來編寫代碼,來進行測試,比如編寫如下的測試代碼,見編寫后的源碼:

#!/usr/bin/env python
#-*-coding:utf-8-*-

import  unittest

def div(a,b):
    return a-b


class DivTest(unittest.TestCase):
    def test_div_001(self):
        self.assertEqual(div(3,2),1)

    def test_div_002(self):
        self.assertEqual(div(3,3),0)

    def test_div_003(self):
        self.assertEqual(abs(div(2,3)),1)
        

 

下面我們通過pytest來執行該文件,見執行后的結果信息,見如下的截圖:

     下面我們結合jenkins,pytest,allure來生成測試報告,在jenekins的系統設置中指定allure的測試報告目錄,見

配置的截圖:

 

我們創建新的job,在構建步驟中選擇windows batch command,填寫執行的命令,見截圖:

在構建后操作選擇Allure Report,見截圖:

 點擊保存,剛才創建的 job是blog,創建成功后,在項目詳情頁面,可以看到顯示allure測試報告的圖標,我們立即構建后,見構建后打印出的信息:

Started by user admin
Building in workspace C:\Users\Administrator\.jenkins\workspace\blog
[blog] $ cmd /c call E:\tools\apache-tomcat-8.0.33\temp\jenkins8291797922995927059.bat

C:\Users\Administrator\.jenkins\workspace\blog>cd D:\git\Python\UnitCI\xpytest 

C:\Users\Administrator\.jenkins\workspace\blog>d:

D:\git\Python\UnitCI\xpytest>python -m pytest test_div.py --alluredir ${WORKSPACE}/report 
============================= test session starts =============================
platform win32 -- Python 2.7.12, pytest-3.2.2, py-1.4.34, pluggy-0.4.0
rootdir: D:\git\Python\UnitCI\xpytest, inifile:
plugins: celery-4.0.2, cov-2.5.1, allure-adaptor-1.7.8
collected 3 items

test_div.py ...

========================== 3 passed in 0.05 seconds ===========================

D:\git\Python\UnitCI\xpytest>exit 0 
[blog] $ C:\Users\Administrator\.jenkins\tools\ru.yandex.qatools.allure.jenkins.tools.AllureCommandlineInstallation\allure\bin\allure.bat generate C:\Users\Administrator\.jenkins\workspace\blog\report -c -o C:\Users\Administrator\.jenkins\workspace\blog\allure-report
Report successfully generated to C:\Users\Administrator\.jenkins\workspace\blog\allure-report
Allure report was successfully generated.
Creating artifact for the build.
Artifact was added to the build.
Finished: SUCCESS

 

見blog的job的詳情頁面,見截圖:

見點擊Allure Report后的測試報告,見截圖:

 

     OK,測試報告就總結到這里,如您對我寫的內容感興趣,可掃描如下二維碼關注我的微信公眾平台,謝謝!

 

 

 

   

 

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM