robotframework 使用redislibrary登錄redis數據庫


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-redislibrary


Uninstalling robotframework-redislibrary-0.1:
  c:\python27\lib\site-packages\redislibrary\__init__.py
  c:\python27\lib\site-packages\redislibrary\__init__.pyc
  c:\python27\lib\site-packages\redislibrary\redislibrarykeywords.py
  c:\python27\lib\site-packages\redislibrary\redislibrarykeywords.pyc
  c:\python27\lib\site-packages\redislibrary\version.py
  c:\python27\lib\site-packages\redislibrary\version.pyc
  c:\python27\lib\site-packages\robotframework_redislibrary-0.1-py2.7.egg-info
Proceed (y/n)? y
  Successfully uninstalled robotframework-redislibrary-0.1

安裝新fork的redislibrary

>> pip install git+https://github.com/penn201500/robotframework-redislibrary.git


Collecting git+https://github.com/penn201500/robotframework-redislibrary.git
  Cloning https://github.com/penn201500/robotframework-redislibrary.git to c:\users\admi
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,>=0.3.0 in c:\python27\lib\site-packages (from
tox->robotframework-redislibrary==0.1)
Requirement already satisfied: py>=1.4.17 in c:\python27\lib\site-packages (from tox->ro
botframework-redislibrary==0.1)
Requirement already satisfied: virtualenv>=1.11.2; python_version != "3.2" in c:\python2
7\lib\site-packages (from tox->robotframework-redislibrary==0.1)
Installing collected packages: robotframework-redislibrary
  Running setup.py install for robotframework-redislibrary ... done
Successfully installed robotframework-redislibrary-0.1

編寫腳本

*** Settings ***
Documentation     測試RF對redis的操作
...               注意!請先在測試環境進行驗證,避免對生產環境進行修改!
...               Maintainer : penn201500@gmail.com

Library           RedisLibrary


*** Variables ***
${key}        some_key_of_redis
${host}           192.168.1.10
${redis_port}     8888
${redis_auth}     password



*** Test Cases ***
test_get_redis_value
    log   獲取redis的key值
    ${redis_conn}=    Connect To Redis    ${host}    ${redis_port}    0    ${redis_auth}
    ${value}=    Get From Redis    ${redis_conn}    ${key}
    log    value is:${value}
    log    修改value,重新獲取
    Set to redis    ${redis_conn}    ${key}    1
    ${new_value}=    Get From Redis    ${redis_conn}    ${key}
    log    new value is:${new_value}  
    log    對取到的value進行判斷    
    should be equal    ${value}    10
    should be equal    ${new_value}    1
    sleep    10s

參考:
http://stackoverflow.com/questions/30149493/redis-auth-command-in-python
https://nottyo.github.io/robotframework-redislibrary/RedisLibrary.html

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