01 - 安装Robot Framework TA环境
根据系统请选择对应的版本包来安装,下面是以Win7-64bit系统为例,来说明如何搭建一个可以运行练习三test case的RF TA环境。
1)首先,要安装好版本对应的python环境,
C:\Users\guowli>python Python 2.7.8 (default, Jun 30 2014, 16:08:48) [MSC v.1500 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>>
在命令行执行python,显示如上信息,则表示python安装成功。
添加环境变量到系统Path:”C:\Python27\;C:\Python27\Lib\site-packages;C:\Python27\Scripts”
2)安装Setuptools和pip,这两个是安装python扩展库的工具,可以在网络连通的情况下,简单快捷的安装所需要的扩展库。
3)安装Robot Framework。可以手工下载源码安装,也可以利用Pip工具来完成robotframework的安装,只需在网络连通的情况下,执行如下命令:”pip install robotframework”
安装完成后,验证一下:
C:\Python27\Scripts>pybot --version Robot Framework 2.8.5 (Python 2.7.8 on win32)
4)安装WxPython和Robot Framework IDE(RIDE)。WxPython是RIDE正常运行所必须依赖的,要先安装,否则在没有WxPython情况下,运行RIDE会出现如下提示:
注意:wxPython必须为2.8.12.1的版本
02 - 创建一个简单的测试用例
Test Case : Invalid and Valid login via SSH
1- Install the test libraries (SSH Library)
请按照如下顺序安装相应library,否则很可能安装失败。
- PyCrypto:Website of Michael Foord. Python programming articles, projects and technical blog.
- ecdsa : ECDSA cryptographic signature library (pure python)
- paramiko : SSH2 protocol library
- robotframework-sshlibrary : Robot Framework test library for SSH and SFTP
2- Create a basic test case
*** Settings *** Library SSHLibrary *** Test Cases *** SSHValidLogin Open Connection 10.68.75.111 22 Loginmcadmin testsc Writedf -h Readdelay=0.5s Start Command pwd ${pwd}Read Command Output Should Be Equal ${pwd}/home/mcadmin Close Connection
3- 获取帮助信息的方法:
- 鼠标:箭头单击相应关键字或参数等, 所指区域背景色会变换为黄色,并显示简要信息
- Ctrl键:选定关键字后,按Ctrl键会显示该关键字的详细信息。
- F5键:在RIDE界面,按F5键 出现”Search Keywords”窗口,可根据需求查找详细信息。也可根据source类别,查看某test library的全部关键字信息
4- Run the test case
03 - 了解Robot Framework标准库
http://robotframework.org/#test-libraries
BuiltIn : Contains generic often needed keywords. Imported automatically and thus always available.
An always available standard library with often needed keywords.
BuiltIn provides a set of generic keywords needed often.
It is imported automatically and thus always available.
http://robotframework.org/robotframework/latest/libraries/BuiltIn.html
OperatingSystem : Enables performing various operating system related tasks.
A test library providing keywords for OS related tasks.
OperatingSystem enables various operating system related tasks to be performed in the system where Robot Framework is running.
http://robotframework.org/robotframework/latest/libraries/OperatingSystem.html
Collections ------ Contains keywords for handling lists and dictionaries.
DateTime ------ Supports creating and verifying date and time values as well as calculations between them.
Dialogs ------ Supports pausing the test execution and getting input from users.
Process ------ Supports executing processes in the system.
Remote ------ Special library acting as a proxy between other libraries running different machines or processes.
Screenshot ------ Provides keywords to capture and store screenshots of the desktop.
String ------ Library for manipulating strings and verifying their contents.
Telnet ------ Supports connecting to Telnet servers and executing commands on the opened connections.
XML ------ Library for verifying and modifying XML documents.
04 - 创建连接数据库的测试用例
测试用例:Robot Framework连接Database,并进行简单操作;
- 较完整的case结构:包含测试集 、测试用例、变量、关键字、资源文件等
- 标准库及扩展库的安装引用
- 可重用的高层关键字
- 资源文件包含变量、关键字
- 资源文件的引用
- 根据相关日志调试,并使之测试通过
1.安装Robot Framework DatabaseLibrary
robotframework-databaselibrary有2个版本:Python和Java版
Python版下载地址:https://pypi.python.org/pypi/robotframework-databaselibrary/
下载并解压缩,然后在命令行,cd到此文件夹下,然后python setup.py install
验证安装是否成功:把DatabaseLibrary加入Library内,然后按F5,看是否出现相关内容
2.安装Python database applications
任何一个Robot Framework的Library基本上都是一个双层结构:
- 外层,实现标准接口供Robot Framework调用;
- 内层,实现具体的功能,提供API供外层进行封装。
Robot Framework DatabaseLibrary只是实现标准接口供Robot Framework调用,为了使它能够真正还需要一个符合Python数据库接口规范的库文件。
两部分缺一不可。
数据库接口:http://wiki.python.org/moin/DatabaseInterfaces
根据需求选择相应的DatabaseInterface下载安装。
示例1:安装MySQL数据库的Database Interfaces
https://wiki.python.org/moin/MySQL
从如上网页内容得知支持MySQL的DB API 2.0 Drivers有如下几种:
- MySQL for Python
- PyMySQL
- mxODBC
- pyodbc 。。。。。。
请根据实际需求(License、Platforms、Python versions、。。。)来选择使用
比如使用PyMySQL:https://github.com/petehunt/PyMySQL
下载包并解压,命令行进入此目录,运行“python setup.py install”即可;
示例2:安装SQLServer的Database Interfaces
https://wiki.python.org/moin/SQL%20Server
从如上网页内容得知支持SQLServer的DB API 2.0 Drivers有如下几种
- adodbapi
- pymssql
- mxODBC
- pyodbc 。。。。。。
请根据实际需求(License、Platforms、Python versions、。。。)来选择使用
比如使用pyodbc:http://code.google.com/p/pyodbc/downloads/list,下载对应的版本并安装;
Pyodbc同时也支持连接mysql,但还需要安装MySQL Connector:http://www.mysql.com/downloads/connector/odbc/
下载对应版本并安装,打开ODBC数据源(控制面板-管理工具-ODBC数据源),点击添加MySQL的ODBC。
安装完成后可以通过pip命令参看包的信息。
3.Test Case示例
通过Robot Framework验证MySQL数据库的一些信息;
Test--Database_Library
*** Settings *** Library DatabaseLibrary Resource test--resource-databaselibrary.txt *** Test Cases *** test_DatabaseLibrary Connect To Database Using Custom Params pymysql host=‘${hostIP}‘, port=${port}, user=‘${user}‘, passwd=‘${password}‘, db=‘mysql‘ ${version} query select version() user-defined compare ${version} ((‘5.0.45‘,),) ${databases} query show databases user-defined compare ${databases} ((‘information_schema‘,), (‘mysql‘,), (‘test‘,)) ${count} Row Count show tables user-defined compare ${count} 17 Table Must Exist user Row Count Is Greater Than X select * from user 1 Disconnect From Database
test--resource-databaselibrary.txt
*** Variables *** ${hostIP} 10.68.75.203 ${port} 3306 ${user} root ${password} password *** Keywords *** user-defined compare [Arguments] ${arg1} ${arg2} [Documentation] convert and compare ${convert} Convert To String ${arg1} Should Be Equal ${convert} ${arg2}
4.问题处理
问题处理-1
运行testcase出现如下错误提示:
FAIL : OperationalError: (2003, ‘Can\‘t connect to MySQL server on \‘10.68.75.203\‘ ((1130, u"Host \‘10.140.1.177\‘ is not allowed to connect to this MySQL server"))‘)
解决方法
提示信息说明账号没有权限连接指定IP的主机,处理方法如下:
[[email protected] ~]# mysql -uroot -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 3 Server version: 5.0.45 Source distribution Type ‘help;‘ or ‘\h‘ for help. Type ‘\c‘ to clear the buffer. mysql> GRANT ALL PRIVILEGES ON *.* TO ‘root‘@‘%‘ IDENTIFIED BY ‘ password ‘ WITH GRANT OPTION; Query OK, 0 rows affected (0.00 sec) mysql>
命令说明
GRANT ALL PRIVILEGES ON *.* TO ‘root‘@‘%‘ IDENTIFIED BY ‘password‘ WITH GRANT OPTION;
含义:允许root用户使用password密码从任何主机连接到mysql服务器。
GRANT ALL PRIVILEGES ON *.* TO ‘root‘@‘ 10.140.1.177‘ IDENTIFIED BY ‘password‘ WITH GRANT OPTION;
含义:只允许root用户使用password密码从ip为10.140.1.177的主机连接mysql服务器。
问题处理-2
运行testcase出现如下错误提示:
FAIL : OperationalError: (2003, ‘Can\‘t connect to MySQL server on \‘10.68.75.203\‘ ((1045, u"Access denied for user \‘root\‘@\‘10.140.1.177\‘ (using password: YES)"))‘)
解决方法:
确认登陆的用户名及密码是否正确,如需更改用户名及密码请按照如下步骤操作:
# 关闭mysql: /etc/init.d/mysql stop 或 service mysqld stop mysqld_safe --user=mysql --skip-grant-tables --skip-networking & mysql -u root mysql mysql> UPDATE user SET Password=PASSWORD(‘新定义的密码‘) where USER=‘root‘; mysql> FLUSH PRIVILEGES; mysql> quit # 启动mysql :/etc/init.d/mysql start 或 service mysqld start mysql -uroot -p 新定义的密码
05 - 创建测试库
参考信息:http://robotframework.org/robotframework/2.8.5/RobotFrameworkUserGuide.html#creating-test-libraries
1-确认要实现的内容
- 公式:(a+b)的a次方,a,b是正整数
- 有一网址(字符串),http://www.example.com?ip=192.187.111.198&code=12345&name=cat,想得到ip内容,即192.187.111.198
- (用户名+10位随机数+一个key)进行md5加密
2-编写test library
测试库以.py为后缀名,文件名与实现该测试库的模块名或者类名相同: <Class name>.py。
根据需要编写testlibrary,定义相关的class及函数等。
返回值在 Python中采用 return 语句。
CreatNewLibrary.py:
# -*- coding: utf-8 -*- import re## re 模块提供了一系列功能强大的正则表达式 (regular expression) 工具 import hashlib## Secure hashes and message digests import random## random 模块包含许多随机数生成器 import string## string 模块提供了一些用于处理字符串类型的函数, class CreatNewLibrary(): def ABA(self,a,b): ‘‘‘ 公式:(a+b)的a次方,a,b是正整数 Example: | ${c} | aba | 2 | 3 | 结果${c}=25 ‘‘‘ return (int(a)+int(b))**int(a) def Find_IP(self,url): ‘‘‘ 从网址获取IP地址 Example: | ${ip} | Find IP | http://www.example.com?ip=192.187.111.198&code=12345&name=cat | 结果${ip}=192.187.111.198 ‘‘‘ ip = re.findall(‘ip=(.*?)&‘,url,re.I) if(ip and ip[0]!=‘‘): return ip[0] else: return "没有匹配到IP" def MD5_RandStr(self,username,key=‘UYTYUT-65HGj-IYR8760-YRJKKL9‘): ‘‘‘ (用户名+一个key)进行MD5加密,key有默认的,也可以执行设定 Example : | ${string} | MD5 RandStr | tester | | ${string} | MD5 RandStr | tester | UYTYUT-65HGj-IYR8760-YRJKKL9 ‘‘‘ return hashlib.md5(username+key).hexdigest()
3-编译调试test library
4- 导入及确认test library
因为将test library文件放在了testcase的同一目录下,所以直接输入文件名全称即可。
如果test library文件在其他地方,要保证文件地址信息正确,或者以查找文件的方式导入也可。
正常的情况下,导入完成test library文件名称在settings中应显示为黑色。
按F5键,选择对应的Source名称,正常情况下,可以看到自定义库和说定义的关键字信息。
5- 应用test library :Test Case 的编写及调试
*** Settings *** Force Tags CreatNewBasicLibrary Library CreatNewLibrary.py *** Test Cases *** CreatNewBasicLibrary ${c}ABA 2 3 Log${c} ${ip}Find IPhttp://www.example.com?ip=192.187.111.198&code=12345&name=cat Log${ip} ${string1} MD5 RandStr tester Log${string1} ${string2} MD5 RandStr tester123456-654321-98765-56789 Log${string2}
执行测试用例
原文地址:https://www.cnblogs.com/anliven/p/10010688.html