1. 程式人生 > 實用技巧 >我的pytest系列 -- pytest+allure+jenkins專案實踐記錄(1)

我的pytest系列 -- pytest+allure+jenkins專案實踐記錄(1)

一次偶然的面試機會,一位面試官給我的啟發,為什麼不用pytest代替unittest做自動化呢?你應該學學pytest,這個對你有好處!故事就從這裡開始,我已經準備好酒了

pytest是什麼

摘自:pytest官網
The pytest framework makes it easy to write small tests, yet scales to support complex functional testing for applications and libraries.
大致意思是:pytest框架更容易做測試框架,還有一大波好用的外掛,大致就是不給你造輪子的機會,在商場裡看到什麼可以用的就可以拿什麼,何樂而不為之,程式碼的世界真好,但還是要勇敢的面對真實世界☺

pytest的好處

01 更少的"模板"
02 更詳細的失敗資訊
03 更靈活的fixture
04 測試引數化
05 小型的測試套
06 整合CI
07 支援外掛
08 活躍的社群
傳送門

Allure是什麼

摘自官網
Allure Framework is a flexible lightweight multi-language test report tool that not only shows a very concise representation of what have been tested in a neat web report form, but allows everyone participating in the development process to extract maximum of useful information from everyday execution of tests.
Allure是一款開源的,專門用來展示測試結果的工具,目的是希望團隊內部的每一個人都可以看到非常清楚的測試報告
Allure可以支援許多的測試工具以及測試框架,下面內容為:Jenkins + pytest框架 + allure
先上個小圖:

目錄

  • 實現的效果展示
  • pytest的使用
  • allure+pytest如何搭配使用
  • 如何整合到jenkins
  • allure如何顯示pytest測試日誌

效果展示

pytest的使用
1、使用pytest的引數化
部分程式碼記錄
test_case.py測試檔案中,
引數使用pytest.mark.parametrize,直接讀取json格式的測試資料物件檔案,根據引數化的方式,發起單元測試

@pytest.mark.parametrize(
    "url,method,headers,body,type,assertContent",
    json_return_dict(get_test_data("A_testcase.json")),
    ids=json_return_ids(get_test_data("A_testcase.json"))
)
def test_fun1(url,method,headers,body,type,assertContent):
    logging.info("發起{0}請求,請求路徑:{1}".format(method, url))
    if method == "GET":
        req = requests.get(url)
        response = req.content
        logging.info("響應結果:{0}".format(response))
        assert True

allure+pytest如何搭配使用
1、需要安裝下載模組
pip install allure-pytest

2、執行測試

執行命令:pytest -s -v -q test_case.py --capture=no --alluredir=./report
./report 表示生成allure報告存放的目錄,生成的檔案可能看不懂,但需要allure-commandline命令執行就能生成對應的測試報告

下載並安裝allure-commandline工具包
使用官網下載安裝包傳送門1使用npm方式下載傳送門2

執行生成好看的報告
命令:allure generate report -o ./allure-result
allure generate report -o ./allure-result --clean
--clean 每次生成清空掉上一次生成的報告記錄

三、如何整合到jenkins (配置篇--pipeline方式)

前提是:jenkins需要安裝支援allure的外掛,步驟:

  1. 在jenkins外掛網站上下載allure外掛最新版本:http://mirrors.jenkins-ci.org/plugins/allure-jenkins-plugin/
  2. 登入Jenkins,在系統管理-> 外掛管理 -> 高階 -> 上傳外掛,進行安裝

    上傳成功並安裝,看到出現一個黃色的球表示已安裝成功,這個時候需要把Jenkins重啟即可使用啦

1、構建一個pipeline工程,Jenkins->新建任務->建立一個pipeline工程

2、pipeline指令碼編寫一個執行pytest的測試步驟,和一個構建完成後的post動作(pipeline的基礎語法,小編建議讀者可以自行學習Jenkins2-pipeline基礎語法)
上程式碼

pipeline {
    agent any
    stages {
        stage('name1'){
            steps{
                
                bat label: '', script: """
                cd api-collection-test\\trunk
                pytest
                """
            }
            
        }
    }
    post {
        always{
            allure includeProperties: false, jdk: '', results: [[path: 'api-collection-test\\trunk\\report']]
        }
    }
}

解釋:

  1. api-collection-test\trunk是我本是jenkins,workspace工作空間的相對的工程目錄
  2. api-collection-test\trunk\report是allure生成的原始報告檔案的目錄,workspace工作空間的相對目錄