搭建本地接口自动化测试环境(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 git@github.com: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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值