使用原因:
① 測試需要跨越多個物理機器,且有的測試庫也必須部署在被測系統上。比如:客戶端需要在兩台系統分別為windows和linux的服務器下,執行測試環境的初始化及清理工作。
② 一個測試要使用多個庫,但是有的只能用jybot運行,有的只能用pybot運行(這種情況很常見)。
遠程庫接口的原理:
為了解決上述兩個難題,Robot Framework提供了遠程庫接口技術(remote library interface)。
什么是遠程庫接口技術呢?其實很簡單,遠程庫接口就是把原來的測試庫變成了三部分
一部分我們可以叫他遠程庫(Remote Library),第二部分叫做遠程服務器(Remote Server),第三部分是真正的測試庫(Test Library)。測試庫提供真正的測試功能,它被遠程服務器包裹起來,通過XML-RPC協議被遠程庫訪問(見下圖)。它的實現思路說白了就是設計模式中的Proxy模式。
操作步驟:
1.服務端操作:
1) 下載robotremoteserver.py並修改:
Robotremoteserver.py為遠程服務腳本,客戶端通過它來調用服務器端的測試庫來執行測試,下載地址如下:
http://robotframework.googlecode.com/hg/tools/remoteserver/robotremoteserver.py
Robotremoteserver.py中需要修改的地方只有一個,為下圖中黃色部分:
def __init__(self, library,host='192.168.8.231', port=8270 , 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()
修改:Robotremoteserver.py需要放置在服務端;
設置host和port為服務器端ip及端口
2) 創建服務器端測試庫(test library),測試庫exampleremotelibrary.py腳本內容如下:
#!/usr/bin/env python import os import sys class ExampleRemoteLibrary: def __init__(self): """Also this doc should be in shown in library doc.""" def start_efastserver(self): os.system('source ./start_efast.sh') def strings_should_be_equal(self, str1, str2): print "Comparing '%s' to '%s'" % (str1, str2) if str1 != str2: raise AssertionError("Given strings are not equal") if __name__ == '__main__': from robotremoteserver import RobotRemoteServer RobotRemoteServer(ExampleRemoteLibrary(), *sys.argv[1:])
上面“start_efastserver”和 “strings_should_be_equal”為在服務端執行操作的函數,例如:腳本中函數start_efastserver 就是在服務端執行命令‘source ./start_efast.sh’,進行開啟EFAST服務操作。
3) 運行服務端測試庫
在服務端執行如下命令:
‘python exampleremotelibrary.py’
客戶端:
4) 測試套(suite)中引入Remote
注意:Remote后面的參數是服務器的ip及端口
5) 在用例中調用遠程測試庫
我們調用strings_should_be_equal這個函數,檢測下:
6)執行用例:
附注:
https://code.google.com/p/robotframework/wiki/RemoteLibrary
本文中使用原因和遠程庫接口的原理來自以下文章:
http://www.ltesting.net/ceshi/ceshijishu/zdcs/robotframework/2012/0305/204291.html