【问题标题】:Is there a way to pass objects connected to hardware to Robot Framework?有没有办法将连接到硬件的对象传递给 Robot Framework?
【发布时间】:2021-03-15 21:29:23
【问题描述】:

我想使用 Robot Framework 测试硬件设备。由于我不想在每个测试用例中再次断开和连接我的设备,我想知道是否有可能在机器人框架之外初始化 Python 对象但在其中使用。

查看我的代码示例如下:

Main.py:

from robot import run
from SomeLibraryUsedByRobot import SomeLibraryUsedByRobot

device = Device()
SomeLibraryUsedByRobot.device = device

run('SomeRobotFile.robot')

SomeLibraryUsedByRobot.py:

class SomeLibraryUsedByRobot:
    device = None

    def access_device(self):
        device.some_function()

SomeRobotFile.robot:

*** Settings ***
Library             SomeLibraryUsedByRobot.py

*** Test Cases ***  
Some Test
    Access Device

主文件的执行导致错误'NoneType' object has no attribute some_function,这让我得出结论,主文件内库的字段device的初始化不起作用。

我也尝试了使用机器人框架的监听器接口及其函数library_import(self, name, attributes)

MyListener.py:

class MyListener:
    ROBOT_LISTENER_API_VERSION = 2

    def library_import(self, name, attributes):
        if name == "SomeLibraryUsedByRobot":
            device = Device()
            SomeLibraryUsedByRobot.device = device
            print( "SomeLibraryUsedByRobot initialized with device" )

当我使用监听器尝试它时,我直接从控制台执行了机器人文件(而不是使用 main.py)。我还在库类中添加了一行:ROBOT_LIBRARY_SCOPE = 'TEST SUITE'

library_import(...) 函数肯定会被调用,因为我在控制台上看到了打印。但是第一次尝试的结果是一样的:'NoneType' object has no attribute some_function

基本上,我们不必在此讨论中涉及任何硬件。我只是想将一些 python 对象从外部传递给机器人框架使用的库。您对此有什么解决方案吗?

【问题讨论】:

    标签: python robotframework


    【解决方案1】:

    SomeLibraryUsedByRobot 中创建一个用于打开/初始化设备的关键字和一个用于关闭/取消初始化设备的关键字。

    class SomeLibraryUsedByRobot:
    
        # Called upon library import for more: http://robotframework.org/robotframework/latest/RobotFrameworkUserGuide.html#library-scope
        def __init__(self):
            self.device = None
    
        def access_device(self):
            self.device.some_function()
    
        def init_device(self):
            self.device = Device()
    
        def deinit_device(self):
            self.device.deinit()
            self.device = None
    

    现在为了避免在每个测试用例中连接、断开连接,请在 Suite Setup/Suite Teardown 中调用这些关键字。

    *** Settings ***
    Library           SomeLibraryUsedByRobot
    Suite Setup       Init Device
    Suite Teardown    Deinit Device
    
    *** Test Cases ***
    Case 1
        Access Device
    
    Case 2
        Access Device
    
    Case 3
        Access Device
    
    Case 4
        Access Device
    

    通常应用在其他现有库中使用的方法,例如 Telnet 或 SeleniumLibrary,其中有 Open ...Close ... 关键字来管理与浏览器的连接,或者在 Telnet 到服务器或硬件的情况下等。

    使用多个设备,您可以使用robot.utils.connectioncache 管理多个连接。许多现有的库都在使用它。

    【讨论】:

      猜你喜欢
      • 2018-01-26
      • 2021-04-13
      • 2013-04-24
      • 1970-01-01
      • 1970-01-01
      • 2014-07-05
      • 1970-01-01
      • 2017-01-30
      • 1970-01-01
      相关资源
      最近更新 更多