简体   繁体   English

将位于 python 文件中的 python 类中的函数导入机器人框架

[英]Import functions from python classes located in python file into Robot Framework

Consider I have this file hierarchy for my project:考虑我的项目有这个文件层次结构:

  • my_project (root folder) my_project(根文件夹)
    • Resources
      • python_file.py containing two classes: FirstClass and SecondClass python_file.py包含两个类: FirstClassSecondClass
    • Tests
      • test_suite.robot

Here are the classes how they look like这是它们的样子

    class FirstClass:
        def my_method:
            pass

    class SecondClass:
        variable = {
              key1: value1,
              key2: value
           }

Firstly, I want to use my_method in .robot file by importing whole class FirstClass using relative path.首先,我想通过使用相对路径导入整个 class FirstClass.robot文件中使用my_method

Secondly, I want to use a dictionary from SecondClass by importing this class somehow (also using relative path).其次,我想通过以某种方式(也使用相对路径)导入这个 class 来使用SecondClass中的字典。

Is this possible in RF?这在射频中可能吗?

Thanks in advance.提前致谢。

While a class might not be directly accessible, its methods can be accessed using objects/instances within python.虽然 class 可能无法直接访问,但可以使用 python 中的对象/实例访问其方法。 To do so, in your Setting you would have to add the python file -为此,在您的设置中,您必须添加 python 文件 -

*** Settings ***
Library    ../Resources/python_file.py

Now within your Test Cases you can invoke the function like any other keyword although it would be advisable to use the file name as well to prevent any confusion to the compiler as follows现在在您的测试用例中,您可以像任何其他关键字一样调用 function,尽管建议使用文件名以及防止编译器混淆,如下所示

*** Test Cases ***
${result}=    python_file.foo #Calls the new function foo that calls my_method

And within the python_file.py create another method -并在 python_file.py 中创建另一个方法 -

def foo():
    obj = FirstClass()
    return obj.my_method()

Or或者

def bar():
    return SecondClass.variable

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

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