Redis 是一个高性能的key-value数据库,在各种企业级应用中经常会遇到。如何使用robotframework处理与redis的自动化交互呢?
还好已有robotframework-redislibrary
安装
安装命令很简单
pip install robotframework-redislibrary
使用
参考文档:https://nottyo.github.io/robotframework-redislibrary/
举例:
登录redis:
${redis_conn}= Connect To Redis 192.168.1.10 port=6379
插入数据:
${data}= Append To Redis ${redis_conn} key data
读取数据:
${data}= Get From Redis ${redis_conn} key
限制及改进
限制
有些redis连接时需要auth,如
使用 nottyo/robotframework-redislibrary 连接redis,出现报错
'StrictRedis' object has no attribute 'auth'
解决办法
google找到的有效解决办法:
http://stackoverflow.com/questions/30149493/redis-auth-command-in-python
简而言之,就是需要在StrictRedis方法中加上password参数,作为登录redis的认证。如
redis.StrictRedis(host='localhost', port=6379, db=0, password=None)
改进方法
修改redislibrary
fork nottyo/robotframework-redislibrary 修改如下:
def connect_to_redis(self, redis_host, redis_port=6379, db=0, redis_password=None):
卸载已安装的redislibrary
>> pip uninstall robotframework-redislibraryUninstalling robotframework-redislibrary-0.1:c:\python27\lib\site-packages\redislibrary\__init__.pyc:\python27\lib\site-packages\redislibrary\__init__.pycc:\python27\lib\site-packages\redislibrary\redislibrarykeywords.pyc:\python27\lib\site-packages\redislibrary\redislibrarykeywords.pycc:\python27\lib\site-packages\redislibrary\version.pyc:\python27\lib\site-packages\redislibrary\version.pycc:\python27\lib\site-packages\robotframework_redislibrary-0.1-py2.7.egg-info
Proceed (y/n)? ySuccessfully uninstalled robotframework-redislibrary-0.1
安装新fork的redislibrary
>> pip install git+https:Collecting git+https:Cloning https:
n\appdata\local\temp\pip-d1xhrz-build
Requirement already satisfied: tox in c:\python27\lib\site-packages (from robotframework
-redislibrary==0.1)
Requirement already satisfied: coverage in c:\python27\lib\site-packages (from robotfram
ework-redislibrary==0.1)
Requirement already satisfied: robotframework>=3.0 in c:\python27\lib\site-packages (fro
m robotframework-redislibrary==0.1)
Requirement already satisfied: redis==2.10.5 in c:\python27\lib\site-packages (from robo
tframework-redislibrary==0.1)
Requirement already satisfied: pluggy<1.0,>&#61;0.3.0 in c:\python27\lib\site-packages (from
tox->robotframework-redislibrary&#61;&#61;0.1)
Requirement already satisfied: py>&#61;1.4.17 in c:\python27\lib\site-packages (from tox->ro
botframework-redislibrary&#61;&#61;0.1)
Requirement already satisfied: virtualenv>&#61;1.11.2; python_version !&#61; "3.2" in c:\python2
7\lib\site-packages (from tox->robotframework-redislibrary&#61;&#61;0.1)
Installing collected packages: robotframework-redislibraryRunning setup.py install for robotframework-redislibrary ... done
Successfully installed robotframework-redislibrary-0.1
编写脚本
*** Settings ***
Documentation 测试RF对redis的操作
... 注意&#xff01;请先在测试环境进行验证&#xff0c;避免对生产环境进行修改&#xff01;
... Maintainer : penn201500&#64;gmail.comLibrary RedisLibrary*** Variables ***
${key} some_key_of_redis
${host} 192.168.1.10
${redis_port} 8888
${redis_auth} password*** Test Cases ***
test_get_redis_valuelog 获取redis的key值${redis_conn}&#61; Connect To Redis ${host} ${redis_port} 0 ${redis_auth}${value}&#61; Get From Redis ${redis_conn} ${key}log value is&#xff1a;${value}log 修改value&#xff0c;重新获取Set to redis ${redis_conn} ${key} 1${new_value}&#61; Get From Redis ${redis_conn} ${key}log new value is&#xff1a;${new_value} log 对取到的value进行判断 should be equal ${value} 10should be equal ${new_value} 1sleep 10s
参考&#xff1a;
http://stackoverflow.com/questions/30149493/redis-auth-command-in-python
https://nottyo.github.io/robotframework-redislibrary/RedisLibrary.html