Saving class objects in robot framework

狂风中的少年 提交于 2019-12-04 22:45:53

Creating suite variables in test cases

The BuiltIn library has a keyword named Set Suite Variable which lets you set a variable that is global for the whole suite. All you need to do is call this after creating your object:

${node}=    Connect To     ${proto}    ${hostname}    ${username}         ${password}    ${port}
Set Suite Variable   ${node}

From that point on, ${node} will be available to all test cases.

Creating suite variables in python code

You can have your library call the same Set Suite Variable keyword from within the library if you don't want the extra step in your test case. For example:

from robot.libraries.BuiltIn import BuiltIn
class AristaLibrary:
    ...
    def connect_to(self, proto, hostname, username, passwd, port):
        ...
        active_conn = pyeapi.connect(proto, hostname, username, passwd, port)
        BuiltIn().set_suite_variable("${node}", active_conn)
        return active_conn

Doing the above will set the variable ${node} when you call the connect_to keyword.

While this is an intriguing solution, it might lead to confusing test cases since it won't be apparent where ${node} is getting set just by reading the test.

See the section named Using Robot Framework's internal modules in the robot framework user's guide for more information about calling keywords from python code.

You need to define the library life cycle to match the testing suite life cycle.

Just define the following at the beginning of your library:

ROBOT_LIBRARY_SCOPE = 'TEST SUITE'

For example,

class ExampleLibrary:

ROBOT_LIBRARY_SCOPE = 'TEST SUITE'

def __init__(self):
    self._counter = 0

def count(self):
    self._counter += 1
    print self._counter

def clear_counter(self):
    self._counter = 0

Every global variable you define in the library, would be available for the library throughout the testing suite.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!