如何使用python在机器人框架中导入和使用用户定义的类
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27603242/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to import and use user defined classes in robot framework with python
提问by Zeinab Abbasimazar
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. I want to make an instance from above class and call its methods. How to do that? I know how to import the lib; it should be like this:
我想用来robotframework
实现我的测试场景。我想从上面的类中创建一个实例并调用它的方法。怎么做?我知道如何导入 lib;它应该是这样的:
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
. Any help?
现在,我想知道如何在robotframework
. 有什么帮助吗?
采纳答案by Laurent Bristiel
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.
回答by user3588820
I have been able to instantiate python classes on-demand (i.e. 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)