Saving class objects in robot framework

2020-08-05 10:58发布

问题:

I am new to Robot Framework. I am writing my own library to work with Robot and I want to save the class object. I want the object to be created and saved once at the suite setup and keep using that same object for the entire test suite. Is there a way to do that?

Aristalibrary.py

import pyeapi

active_conn = None


class AristaLibrary:
    def __init__(self, proto="https", hostname='localhost',
                 username="admin", passwd="admin", port="443"):
        self.hostname = hostname
        self.proto = proto
        self.port = port
        self.username = username
        self.passwd = passwd

    def connect_to(self, proto, hostname, username, passwd, port):
        proto = str(proto)
        hostname = str(hostname)
        username = str(username)
        passwd = str(passwd)
        port = str(port)
        active_conn = pyeapi.connect(proto, hostname, username, passwd, port)
        return active_conn

    def enable(self, conn, command):
        return conn.execute([command])

    def get_active_connection(self):
        return active_conn

loginsight_sshlib_demo.txt

*** Setting ***
Library    BuiltIn
Library    String
Library    AristaLibrary

*** Variables ***
${hostname}    192.168.117.20
${username}    admin
${password}    admin
${port}        80
${proto}       http

*** Test Cases ***

Test Aristalibrary
    ${node}=    Connect To     ${proto}    ${hostname}    ${username}         ${password}    ${port}
    LOG    ${node}    level=DEBUG

Test Persistance of variables
    ${node}=     Get Active Connection     
    ${output}=    Enable    ${node}    show version
    LOG    ${output}    level=DEBUG

*** Keywords ***
Open Connection and Login
    Open Connection    ${hostname}
    Login    ${username}    ${password}
    Write     enable
    ${Output}=    Read 
    Log    ${Output}    level=INFO

回答1:

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.



回答2:

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.