天天看點

Python+Request+Allure進行API接口測試自動化(一)

       簡單記錄一下我嘗試進行接口自動化測試的過程。一個基本目标就是能夠用python來寫接口測試代碼,并且要做到足夠簡單,能夠讓不太懂python語言的人也可以很容易參照例子完成接口的測試用例編寫,并且和jenkins能夠內建起來自動反複執行,測試結果可以圖表的方式顯示出來。開發和部署都是用Windows系統,期間有碰到很多坑,網上的資料太零散了,要多方參考和收集才能得到滿意的結果。

(一)python環境準備

1、安裝Python3;

https://www.python.org/downloads/

2、下載下傳Python開發IDE工具pycharm;

http://www.jetbrains.com/pycharm/download/

注冊碼:http://idea.lanyus.com

3、安裝pip3

https://pypi.org/project/pip/

4、pip3來安裝python常用子產品

pip install -U pytest

pip install -U requests

pip install -U requests

pip install -U hashlib

pip install -U json

5、檢查PowerShell

以管理者身份營運 Powershell

在PowerShell中運作 $PSVersionTable 來看版本号,要求版本号大于3

6、安裝allure

https://docs.qameta.io/allure/#_installing_a_commandline

7、內建pytest+allure

https://docs.qameta.io/allure/#_pytest

(二)Jenkins環境準備

1、安裝Java的JDK;

https://www.oracle.com/technetwork/java/javase/downloads/index.html

2、下載下傳jenkins的war包;

http://mirrors.jenkins-ci.org/windows/latest

3、下載下傳tomcat

https://tomcat.apache.org/download-80.cgi

修改tomcat/conf/logging.properties檔案中的*encoding對應的編碼為 UTF-8

4、jenkins插件

Jenkins的插件是一個大坑,由于國内的網絡被屏蔽了,直接用預設的Site是無法下載下傳插件的,這裡就折騰了很久。

系統管理-》插件管理-》Advanced

Update Site :https://mirrors.tuna.tsinghua.edu.cn/jenkins/updates/update-center.json

安裝 allure/html/Git/Pipeline/Python/Ant/maven/Github/Email/PMD/Findbug等插件

5、jenkins全部配置

系統管理-》全局工具配置 配置 Maven、JDK、Python、Allure Commandline、Ant 等

(三)API接口測試(特别重要)

1、建一個自由風格的軟體項目

2、源代碼管理

Source Code Management :Subversion

2、Build

Windows批處理任務:

pytest -s -q --alluredir allure-results --html html\test.html

exit 0

3、Post-build Actions

Results->Path: allure-results

Report path:allure-report