1.robotframework和loadrunner集成
一般我们进行完功能测试,都需要进行下性能测试,那么这章我来介绍下,RobotFramework与loadrunner性能测试的融合,即运行完自动化功能测试,借助RobotFramework的Remote库来执行性能测试。
1.1准备条件
A:一台pc,系统win7,安装有python、RobotFramework及ride。
B:一台pc,安装有loadrunner,python,IP为192.168.3.124
C:一台服务器
1.2结构图
1.3在loadrunner机器上安装robotremoteserver.
下载robotremoteserver.py(Remote Server)并修改
Robotremoteserver.py为远程服务脚本,客户端通过它来调用服务器端的测试库来执行测试,下载地址如下:
http://robotframework.googlecode.com/hg/tools/remoteserver/robotremoteserver.py
或者https://github.com/robotframework/PythonRemoteServer
> pip install robotremoteserver
把Robotremoteserver.py和Exampleremotelibrary.p.py存放到C:\lr_get_script\rf_lr
都放置在安装loadrunner的PC上。
Robotremoteserver.py中需要修改的地方,就是host和port:
def __init__(self, library, host='192.168.8.231',port=8277, allow_stop=True):
SimpleXMLRPCServer.__init__(self, (host, int(port)), logRequests=False)
self._library = library
self._allow_stop = allow_stop
self._register_functions()
self._register_signal_handlers()
self._log('Robot Framework remote server starting at %s:%s'
% (host, port))
self.serve_forever()
修改: 设置host和port为安装loadrunner的测试机ip及端口
run_performance_test函数为调用loadrunner的wlrun.exe,执行给出的场景脚本。
Exampleremotelibrary.py
import os
import sys
class ExampleRemoteLibrary:
"""Example library to be used with Robot Framework'sremote server.
Thisdocumentation is visible in docs generated by _libdoc.py_
starting from Robot Framework 2.6.2.
"""
def__init__(self):
"""Also this doc should be in shown in librarydoc."""
defrun_performance_test(self,scriptname):
run_pfm_str = ''
run_pfm_list = ["wlrun.exe -TestPath ",scriptname," -port8080 -Run -DontClose"]
#运行完loadrunner后不关闭
#run_pfm_list= ["wlrun.exe -TestPath ",scriptname," -port 8080 -Run "]
#运行完之后自动关闭,结果在control里面配置成自动保存
os.chdir("C:\\Program Files (x86)\\Hp\\LoadRunner\\bin")
os.system(run_pfm_str.join(run_pfm_list))
if __name__ == '__main__':
fromrobotremoteserver import RobotRemoteServer
RobotRemoteServer(ExampleRemoteLibrary(), *sys.argv[1:])
1.4运行服务端测试库
在安装loadrunner的PC上执行如下命令:
>c:\lr_get_script\rf_lr>pythonExampleremotelibrary.p.py
Robot Framework remote server at192.168.3.124:8277 started.
1.5 在另外一台机器上rf上先加入Remote库
注意:Remote后面的参数192.168.3.124:8270是测试执行机(安装loadrunner的PC)的ip及端口
1.6 用例写作及执行,在用例中调用远程测试库
我们调用run_performance_test这个函数
运行用例后自动打开lr后开始自动跑性能测试
2.远程服务器端192.168.3.124创建远程python测试库(Test Library)
2.1TestLibrary.py
#coding=utf-8
import sys
from robotremoteserver importRobotRemoteServer
__version__ = '1.0'
class TestLibrary(object):
#def __init__(self):
# pass
def Test_add(self,a,b):
if(a>b):
falg=False
return False
else:
flag=True
return flag
def test1(self):
"""test 1"""
print "test1"
def test2(self):
"""test2"""
print "test1"
def test3(self):
"""test3"""
print "test3"
if __name__ == '__main__':
RobotRemoteServer(TestLibrary(),'192.168.3.124','8270')
2.2__init__.py (rf自定义库)
# coding=utf-8
from TestLibrary import TestLibrary
version = '1.0'
class TestLibrary(TestLibrary):
ROBOT_LIBRARY_SCOPE = 'GLOBAL'
2.3 配置robotremoteserver.py
通过>pip install robotremoteserver
或者https://github.com/robotframework/PythonRemoteServer 下载robotremoteserver安装包后安装
把C:\Python27\Lib\site-packages\Robotremoteserver.py
复制到C:\Python27\Lib\site-packages\TestLibrary\Robotremoteserver.py
然后编辑Robotremoteserver.py
class RobotRemoteServer(object):
def __init__(self, library,host='192.168.3.124', port=8270, port_file=None,
allow_stop='DEPRECATED',serve=True, allow_remote_stop=True):
"""Configure and start-up remote server.
192.168.3.124为远程服务器ip地址
port=8270为对应的服务器端口
2.4 运行TestLibrary.py
C:\Python27\Lib\site-packages\TestLibrary>pythonTestLibrary.py
Robot Framework remote server at192.168.3.124:8270 started.
在Server端运行此py文件,只有先在Server端运行起来该库文件,在RobotFramework 控制端才可以添加Remote库。
被远程控制的Server端的防火墙要关闭
2.5 RobotFramework 控制端添加Remote库,过程如下,alias可写可不写,当Server端库文件很多的时候,要写上作为区分:
2.6在另外一台安装有rf的机器里面配置库创建测试用例
新建的testcase里,就可以使用自定义的MyLibrary中的关键字了,如test1,test2,test3
创建exampleremotelibrary.py脚本,脚本内容如下:
import os
import sys
class ExampleRemoteLibrary:
"""Example library to be used with Robot Framework'sremote server.
This documentation is visible in docs generated by _libdoc.py_
starting from Robot Framework 2.6.2.
"""
def __init__(self):
"""Also this doc should be in shown in librarydoc."""
defrun_performance_test(self,scriptname):
run_pfm_str= ''
run_pfm_list= ["wlrun.exe -TestPath ",scriptname," -port 8080 -Run-DontClose"]
os.chdir("C:\\Program Files (x86)\\Hp\\LoadRunner\\bin")
os.system(run_pfm_str.join(run_pfm_list))
if __name__ == '__main__':
from robotremoteserver import RobotRemoteServer
RobotRemoteServer(ExampleRemoteLibrary(), *sys.argv[1:])
run_performance_test函数为调用loadrunner的wlrun.exe,执行给出的场景脚本。




