1. 程式人生 > 實用技巧 >Jenkins自動構建-部署-測試

Jenkins自動構建-部署-測試

大概的思路:

1.Jenkins拉取git程式碼,自動構建

2.構建成功後,將程式/jar包上傳到測試伺服器,並在測試伺服器執行部署指令碼

3.以上完成後,觸發自動化測試,由jenkins中的另一個任務觸發

4.例如在一臺jenkins 的slave上(windows)觸發執行自動化測試指令碼,測試完成後,將報告發送到Jenkins中設定的人員以及在jenkins中展示

一。jenkins中的設定

郵件通知的設定

注:這裡的Password不是郵箱的密碼,而是郵箱的授權碼,可以百度下怎麼獲取

測試伺服器的設定

設定完畢後,可以點選test configuration進行測試,檢視是否能連得到

主工程的設定(需要構建的專案)

引數化構建,通過提交的分支或者tag進行部署,當然也可以選擇revision

原始碼的配置

構建指令碼

rm -rf ./dist
rm -rf ./dist.zip
npm install
npm run build
cd $WORKSPACE/
echo "GIT_COMMIT: " > dist/git.txt
echo $GIT_COMMIT >> dist/git.txt
echo -e "\nGIT_URL: " >> dist/git.txt
echo ${GIT_URL#*xxxxxx.com.cn} >> dist/git.txt

zip 
-r dist.zip dist

前端專案,通過npm進行打包,另外需要將當前的版本號打包進去

構建完成後需要執行的內容

歸檔成品,也可以不需要

執行的另一個project,也就是執行自動化測試的project

通過publish over ssh將dist.zip傳到我們的測試伺服器並執行部署指令碼,需要注意執行的指令碼的許可權問題

自動化測試工程的設定

指定在哪一臺slave執行,windows是我建立的一臺windows測試機

原始碼我沒有設定,因為只是臨時搭建下,我直接把測試程式碼放在了測試機上,實際上,應該通過svn或者其他方式拉取到測試機上,每次執行自動化測試的時候拉取最新的測試程式碼

執行自動化測試指令碼

jenkins中的報告展示,設定報告的路徑

設定郵件通知

獨立設定的收件人郵箱

郵件的正文以及將日誌和測試報告作為附件

二。測試程式碼

和之前的思路也差不多,unittest、htmltestrunner這些

新加的東西:將每個頁面的元素都配置在一個py中,引入蟲師的poium

如果能通過name或者id定位到元素最好,xpath不是很穩定(前端佈局修改時,xpath可能會造成變更)

#coding:utf-8
from poium import Page,PageElement,PageWait
class loginPage(Page):
    userNameInput = PageElement(xpath = '//*[@id="app"]/div/div/form/div[1]/div/div[1]/input',timeout=5,describe='登入使用者名稱輸入框')
    passwordInput = PageElement(xpath = '//*[@id="app"]/div/div/form/div[2]/div/div[1]/input',timeout=5,describe='使用者密碼輸入框')
    loginButton = PageElement(xpath = '//*[@id="app"]/div/div/form/div[3]/div/button',describe='登入按鈕')
    showPWDButton = PageElement(xpath = '//*[@id="app"]/div/div/form/div[2]/div/span',describe='明密文切換按鈕')

判斷case是否通過的方式,從檢查特定元素是否存在改為,預設正常情況下的螢幕截圖,和實際執行操作時的螢幕截圖進行對比,通過相似度進行判斷,相似度的設定需要自己斟酌下

#coding:utf-8
import os
from PIL import Image
import math
import operator
from functools import reduce

def takeDiff(pic1,pic2):
    image1 = Image.open(pic1)
    image2 = Image.open(pic2)
    histogram1 = image1.histogram()
    histogram2 = image2.histogram()
    differ = math.sqrt(reduce(operator.add, list(map(lambda a, b: (a - b) ** 2, histogram1, histogram2))) / len(histogram1))
    if differ <19:
        return True
    else:
        return False

將一個模組的用例放在一個py檔案中(只是我的做法是這樣)

#coding:utf-8
import unittest
from selenium import webdriver
from HTMLTestRunner_cn import HTMLTestRunner
import time
from common import common,base_page
import os
#截圖存放位置
spath = r'C:/python/BI_auto/screenshot/login'
class LoginTest(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Chrome()
        self.loginPage = base_page.loginPage(self.driver)
        self.homePage = base_page.homePage(self.driver)
        self.loginPage.get('xxxxxxx.com.cn/#/login')
        self.driver.maximize_window()

    def tearDown(self):
        self.driver.close()

    def add_img(self):
        self.imgs.append(self.driver.get_screenshot_as_base64())
        return True

    def cleanup(self):
        pass

    def test_login_03(self):
        u'''檢查顯示密碼按鈕'''
        try:
            self.loginPage.passwordInput = 'admin'
            self.loginPage.showPWDButton.click()
            time.sleep(3)
            self.driver.get_screenshot_as_file(spath + '點選顯示密碼按鈕.png')
            #截圖對比結果,如果差異值小於9則認為相同(admin密碼明密文顯示差異值在10)
            diffResult = common.takeDiff(spath + '點選顯示密碼按鈕.png',r'C:/python/BI_auto/testPic/login_03.png')
            #刪除用作對比時的截圖
            os.remove(spath + '點選顯示密碼按鈕.png')
            #self.assertEqual(diffResult,1,'點選顯示密碼按鈕,密碼沒有變為明文')
            self.assertTrue(diffResult,'點選顯示密碼按鈕,密碼沒有變為明文')
        except:
            #self.driver.get_screenshot_as_file(spath+'03_點選顯示密碼,密碼沒有變為明文.png')
            self.add_img()
            raise

我設定的路徑都是絕對路徑,應該用相對路徑

對元素的操作完畢後,截圖,和預設的圖片進行對比,對比完成後將截圖刪除,再對比較結果進行判斷

如果相似度過低,則將現在的螢幕截圖儲存到報告中

用例的執行統一放到all_test中

#coding=utf-8
import unittest
import sys
import os
sys.path.append("/test_case")
#新增test_case目錄
from testcase import login,importDB
#import HTMLTestRunner
from HTMLTestRunner_cn import HTMLTestRunner
import time

alltestnames = [login.LoginTest]
# alltestnames = [login.LoginTest,
#                 importDB.ImportDBTest
#                 ]

testunit = unittest.TestSuite()

for test in  alltestnames:
    testunit.addTest(unittest.makeSuite(test))

timestr = time.strftime("%Y-%m-%d-%H_%M_%S",time.localtime(time.time()))
#filename = r'C:/python/BI_auto/report/'+timestr+ r'.html'
filename = r'C:/workspace/bi_UI_auto/report/'+ r'newest.html'
fp = open(filename,'wb')
runner = HTMLTestRunner(
    stream=fp,
    title='BI測試報告',
    description='report',
    verbosity=2
)
runner.run(testunit)
fp.close()
print('執行完畢,報告路徑:'+filename)

一樣,路徑的問題需要自己再調整下,jenkins中自動化測試的project中需要做的也就是獲取最新的測試程式碼(需要修改程式碼中的路徑設定)然後執行這個all_test.py

三。執行結果

構建、部署、測試完畢後,會有郵件傳送

在jenkins中也能看到本次的報告

執行失敗的用例,點選顯示截圖能看到測試時的截圖