在机器人框架中保存类对象 [英] Saving class objects in robot framework

查看:118
本文介绍了在机器人框架中保存类对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Robot Framework的新手。我正在编写我自己的库以使用Robot和我想保存类对象。我想要的对象被创建和保存一次在套件设置,并保持使用同一个对象的整个测试套件。有办法吗?

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

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

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


推荐答案



BuiltIn库有一个关键字设置套件变量,它允许您为整个套件设置一个全局变量。所有你需要做的是在创建你的对象后调用这个:

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}


$ b b

从那时起, $ {node} 将可用于所有测试用例。

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

您可以让库在库中调用相同的 Set Suite Variable 关键字if你不想在你的测试案例中的额外步骤。例如:

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

当您调用 connect_to 关键字时,执行上述操作将设置变量 $ {node}

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

虽然这是一个有趣的解决方案,但它可能会导致混淆测试用例,因为它不会在 $ {node}

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.

请参阅在机器人框架用户指南中使用Robot Framework的内部模块,了解关于从python代码调用关键字的更多信息。

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.

这篇关于在机器人框架中保存类对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆