搭建本地接口自動化測試環境(robotframework)

一、安裝python環境

  • 訪問python官網,下載python安裝包(當前爲3.8.0,建議使用3.7.5,較新版本可能依賴庫不能及時更新)
    下載地址
    下載地址

  • 安裝python
    基本一路next(下一步)到最後。注意勾選Add PythonXXX to PATH,其他選
    項根據需要勾選或去勾選

  • 測試是否安裝成功
    打開命令行cmd,輸入"python -V"(注意是大V),回車。如果展示python的版本即爲安裝成功
    python版本

  • 建立本地虛擬環境Virtualenv(按需)

$ pip3 install virtualenv
$ virtualenv ${your_project_name} -p python3 --no-site-packages
$ cd Scripts/bin # windows和linux系統目錄有所區別
$ source activate # windows執行activate.bat文件即可
 
執行後:如下()裏是your_project_name,標識切換環境成功
C:\virtualenv\market-apitest\Scripts
(market-apitest) λ
  • 配置下載源(官方源對於國內用戶下載可能偏慢)
    pip國內的一些鏡像
    阿里雲 http://mirrors.aliyun.com/pypi/simple/
    中國科技大學 https://pypi.mirrors.ustc.edu.cn/simple/
    豆瓣(douban) http://pypi.douban.com/simple/
    清華大學 https://pypi.tuna.tsinghua.edu.cn/simple/
    中國科學技術大學 http://pypi.mirrors.ustc.edu.cn/simple/

在用戶目錄(%HOMEPATH%)下,幾C:\Users\xxx創建pip目錄,在pip目錄下創建pip.ini文件,文件內容如下:

[global]
timeout = 6000
index-url = https://mirrors.aliyun.com/pypi/simple/
 
[install]
trusted-host = mirrors.aliyun.com

二、安裝robotframework環境

  • 先安裝robotframework
pip install -U robotframework

安裝完,確認下是否安裝成功
在這裏插入圖片描述

  • 安裝RIDE(一個編輯器,可以快速編碼)
pip install -U robotframework-ride==1.7.3.1

在這裏插入圖片描述

  • 啓動RIDE編輯器
    命令行輸入:ride.pypython ride.py./run.sh ride.py (run.sh見下文常見報錯解決方法)。成功打開,界面如下:
    在這裏插入圖片描述

三、安裝依賴庫

安裝發http請求依賴庫(requestsLibrary
RF目前提供兩個依賴庫,如下:
在這裏插入圖片描述
區別:左邊的是python語言封裝實現的(官網),右邊的是java語言封裝實現的,在以後的教程中,均以python的來示例(RF本身就是由python實現的)。

pip install -U requests
pip install -U robotframework-requests

安裝完,確認下是否安裝成功(pip list)
在這裏插入圖片描述
題外話:
requests:是python的一個非常人性化的http庫(https://requests.readthedocs.io/en/master/),其上手快,功能強大,比同類庫如urlllib2(python2)、urllib(python3)都很強大。
robotframework-requests是基於requests模塊封裝的RF庫,我們在做接口自動化時將使用該庫,即requestsLibrary

常見報錯解決辦法

  • ERROR: Could not find a version that satisfies the requirement Pywin32 (from robotframework-ride) (from versions: none)
    ERROR: No matching distribution found for Pywin32 (from robotframework-ride)
    在這裏插入圖片描述
    解決方法:
git clone [email protected]:robotframework/RIDE.git
cd RIDE
python setup.py build
python setup.py install

也可以通過下載離線安裝包,通過pip install xxx.whl安裝

  • ModuleNotFoundError: No module named ‘wx’
    AttributeError: ‘ModuleNotFoundError’ object has no attribute ‘message’
    在這裏插入圖片描述
    解決方法:
pip install -U wxPython
  • This program needs access to the screen. Please run with a
    Framework build of python, and only when you are logged in
    on the main display of your Mac.
    上述報錯因爲wxpython裝在virtualenv中,解決方法如下(在虛擬環境bin目錄下創建run.sh):
    PYVER,爲你本地安裝的版本,請根據實際安裝版本填寫
# what real Python executable to use
PYVER=3.7
PYTHON=/Library/Frameworks/Python.framework/Versions/$PYVER/bin/python$PYVER
 
# find the root of the virtualenv, it should be the parent of the dir this script is in
ENV=`$PYTHON -c "import os; print(os.path.abspath(os.path.join(os.path.dirname(\"$0\"), '..')))"`
 
# now run Python with the virtualenv set as Python's HOME
export PYTHONHOME=$ENV
exec $PYTHON "$@"

run.sh加可執行權限,chmod u+x run.sh,啓動RIDE。./run.sh ride.py

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