简体   繁体   English

如何使用python在机器人框架中导入和使用用户定义的类

[英]How to import and use user defined classes in robot framework with python

Assume I have a class in python:假设我在 python 中有一个类:

class TestClass(object):
    def __init__(self, arg1, arg2):
        self.arg1 = arg1
        self.arg2 = arg2

    def print_args(self):
        print arg1, arg2

I want to use robotframework to implement my tests scenarios.我想使用robotframework来实现我的测试场景。 I want to make an instance from above class and call its methods.我想从上面的类中创建一个实例并调用它的方法。 How to do that?怎么做? I know how to import the lib;我知道如何导入 lib; it should be like this:它应该是这样的:

Library   TestClass

I don't know how to initialize an object from this class and call class methods via this object.我不知道如何从这个类初始化一个对象并通过这个对象调用类方法。 If I wanted to implement it with python, I would write some piece of code like this:如果我想用python来实现它,我会写一些这样的代码:

import TestClass
test = TestClass('ARG1', 'ARG2')
test.print_args()

Now, I want to know how to write this in robotframework .现在,我想知道如何在robotframework编写它。 Any help?有什么帮助吗?

To import the library with arguments, just add them after the library name :要导入带参数的库,只需在库名称后添加它们

Library  TestClass  ARG1  ARG2

So the "import" and the instantiation are done in one shot.所以“导入”和实例化是一次性完成的。 Now, the thing that can be tricky is to understand the scope of your instance.现在,棘手的事情是了解您的实例的范围。 This is well explained in the User Guide section " Test Library Scope ":这在用户指南部分“ 测试库范围”中有很好的解释:

A new instance is created for every test case.为每个测试用例创建一个新实例。 [...] This is the default. [...] 这是默认设置。

Note that if you want to import the same library several times with different arguments, and hence have difference instances of your classes, you will have to name them on import:请注意,如果您想使用不同的参数多次导入同一个库,因此您的类有不同的实例,则必须在导入时命名它们:

Library  TestClass  ARG1  ARG2  WITH NAME  First_lib
Library  TestClass  ARG3  ARG4  WITH NAME  Second_lib

And then in your tests, you have to prefix the keywords:然后在您的测试中,您必须为关键字添加前缀:

*** Test Cases ***
MyTest
    First_lib.mykeyword  foo  bar
    Second_lib.mykeyword  john  doe

This is explained in this section of the User Guide . 用户指南的这一部分对此进行了解释。

I have been able to instantiate python classes on-demand (ie not just hard-coded args as via the Library technique).我已经能够按需实例化 python 类(即不仅仅是通过库技术进行硬编码的参数)。

I used a helper method to create the class.我使用了一个辅助方法来创建这个类。 I was unable to get the Robot script to call the class constructor directly, however it is able to call functions in Python, so we can create a class or namedtuple by providing a function-based interface:我无法让 Robot 脚本直接调用类构造函数,但是它可以在 Python 中调用函数,因此我们可以通过提供基于函数的接口来创建类或命名元组:

File: resource_MakeMyClass.robot文件: resource_MakeMyClass.robot

*** Settings ***
Library             myClass

*** Keywords ***
_MakeMyClass
    [Arguments]    ${arg1}    ${arg2}
    ${result} =    makeMyClass    ${arg1}    ${arg2}
    [Return]       ${result}

File: myClass.py文件: myClass.py

class MyClass(object):
    def __init__(self, arg1, arg2):
        self.arg1 = arg1
        self.arg2 = arg2

def makeMyClass(arg1, arg2):
    return MyClass(arg1, arg2)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM