在写Robot Framework 的Case时,我们一般采用的都是相对路径引用我们的lib以及其他资源文件,因此需要给Robot添加一个搜索路径,但是采用命令行和RIDE还有一些差别。
这里举例简单说明一下。
例如:
Import | Name/Path |
library | libName.py |
假如,你的lib库放在TestSuite的Libraries文件夹下,你希望通过把这个文件夹添加到PYTHONPATH环境变量中,让Robot在运行时能够能够查找到keyword的python库文件。
但是对于非lib库的具体的一些资源文件,最好是根据其功能分别保存在特定的文件路径下,需要写一个脚本设置Robot对应具体文件夹的全局环境变量。
具体方法:根据当前TestSuite文件所在路径,向上获取对应的资源文件路径,并且设置对应的环境变量。
下面用一个比较典型的例子说明:
加入你执行的Case需要的资源根据目录结构层次展开为多个文件夹,因此可以用一个python脚本来设置这些所需的环境变量,以便于Robot执行时所需。
(代码的显示格式是从MSDN参考过来的,看起来还不错。)
import os import os.path import sys import time from robot.variables import GLOBAL_VARIABLES class GlobalEnvSettings: def __init__(self): """ GlobalEnvSettings: performs initial test environment setup Following global variables are set if corresponding folders exist: ${BUILD_ROOT} - path to the build ('C_Test' folder position) ${BUILD_BINARIES} - path to the build's binary files ${ENV_ROOT} - Robot test environment path ${ENV_LIBRARIES} - path to generic test env libraries ${ENV_TESTS} - path to test env tests ${ENV_VECTORS} - path to test vectors ${ENV_MESSAGES} - path to test env messages ${ENV_TEMPLATES} - path to test env message templates ${ENV_TESTLIBRARIES} - path to test specific libraries ${ENV_REPORTPATH} - path to the current report folder ENV_LIBRARIES and ENV_TESTLIBRARIES are added to the OS PATH environment variable so there is no need to add these paths while importing libraries """ print "Setting up folders and paths" v_currentPath = os.path.abspath(os.path.dirname(__file__)) v_buildRootPos = v_currentPath.find("Test_Branch_1") if v_buildRootPos < 0: v_buildRootPos = v_currentPath.find("Test_Branch_2") if v_buildRootPos < 0: v_buildRootPos = v_currentPath.find("Test_Branch_3") if v_buildRootPos < 0: v_buildRootPos = v_currentPath.find("Test_Branch_4") if v_buildRootPos < 0: v_buildRootPos = v_currentPath.find("Test_Branch_5") if v_buildRootPos >= 0: v_buildRootPath = v_currentPath[:v_buildRootPos].rstrip("\\") GLOBAL_VARIABLES["${BUILD_ROOT}"] = v_buildRootPath print "Build root: '%s'" % v_buildRootPath v_binariesPath = v_buildRootPath + "\\SomePath\\Bin" if os.path.exists(v_binariesPath): GLOBAL_VARIABLES["${BUILD_BINARIES}"] = v_binariesPath print "Build's binaries folder: '%s'" % v_binariesPath else: raise Exception,'no directory found'
v_testEnvPath = v_currentPath + "\\" v_testEnvRootPos = v_testEnvPath.rfind("\\Robot\\") if v_testEnvRootPos < 0: raise Exception,'no directory found'v_testEnvRootPos = v_testEnvPath.rfind("\\Robot\\") if v_testEnvRootPos >= 0: v_testEnvRootPath = v_testEnvPath[:v_testEnvRootPos] + "\\Robot" GLOBAL_VARIABLES["${ENV_ROOT}"] = v_testEnvRootPath print "Test environment root: '%s'" % v_testEnvRootPath v_librariesPath = v_testEnvRootPath + "\\Libraries" if os.path.exists(v_librariesPath): GLOBAL_VARIABLES["${ENV_LIBRARIES}"] = v_librariesPath print "Test environment global libraries folder: '%s'" % v_librariesPath v_testsPath = v_testEnvRootPath + "\\Tests" if os.path.exists(v_testsPath): GLOBAL_VARIABLES["${ENV_TESTS}"] = v_testsPath print "Test environment tests folder: '%s'" % v_testsPath v_vectorsPath = v_testEnvRootPath + "\\Vectors" if os.path.exists(v_vectorsPath): GLOBAL_VARIABLES["${ENV_VECTORS}"] = v_vectorsPath print "Test environment vectors folder: '%s'" % v_vectorsPath v_messagesPath = v_testEnvRootPath + "\\Messages" if os.path.exists(v_messagesPath): GLOBAL_VARIABLES["${ENV_MESSAGES}"] = v_messagesPath print "Test environment messages folder: '%s'" % v_messagesPath v_templatesPath = v_messagesPath + "\\Templates" if not os.path.exists(v_templatesPath): os.mkdir(v_templatesPath) GLOBAL_VARIABLES["${ENV_TEMPLATES}"] = v_templatesPath print "Test environment message templates folder: '%s'" % v_templatesPath v_testsLibrariesPath = v_testsPath + "\\Libraries" if os.path.exists(v_testsLibrariesPath): GLOBAL_VARIABLES["${ENV_TESTLIBRARIES}"] = v_testsLibrariesPath print "Test environment test libraries folder: '%s'" % v_testsLibrariesPath v_testsResourcesPath = v_testsPath + "\\Resources" if os.path.exists(v_testsResourcesPath): GLOBAL_VARIABLES["${ENV_TESTRESOURCES}"] = v_testsResourcesPath print "Test environment test resources folder: '%s'" % v_testsResourcesPath if GLOBAL_VARIABLES.has_key("${LOG_FILE}"): v_reportPath = os.path.dirname(GLOBAL_VARIABLES["${LOG_FILE}"]) GLOBAL_VARIABLES["${ENV_REPORTPATH}"] = v_reportPath print "Test environment reports folder: '%s'" % v_reportPath if v_librariesPath != "": sys.path.append(v_librariesPath) if v_testsLibrariesPath != "": sys.path.append(v_testsLibrariesPath) time.clock() def dummy_method(self): """ Dummy method to avoid Robot warning that library has no keywords """ pass if __name__ == "__main__": k = GlobalEnvSettings() for i_gloVar in GLOBAL_VARIABLES.iteritems(): print i_gloVar[0] + ":" + i_gloVar[1]
注释:
1.
__file__ is to be the “path” to the file unless the module is built-in (and thus listed in
sys.builtin_module_names) in which case the attribute is not set. If what is being imported is a package then
__path__ is to be set to a list of paths to be searched when looking for modules and packages contained within the package being imported.
import os import os.path print "Setting up folders and paths" v_currentPath = os.path.abspath(os.path.dirname(__file__)) print v_currentPath
需要注意的是,如果你直接在Python IDLE中运行该脚本,一定会提示运行失败,原因是:
NameError: name '__file__' is not defined
只有在命令行运行才能获取正确的路径结果。