RobotFramework: 設置Case執行的搜索路徑

在寫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

只有在命令行運行才能獲取正確的路徑結果。




發佈了96 篇原創文章 · 獲贊 26 · 訪問量 14萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章