0

我正在导入一个 python 库,并希望创建两个具有不同参数的对象,并调用类中定义的方法。

演示.py

class Sample:

    def __init__(self,path,device):
            self.device=device
            self.path = path
            print(self.device)
            print(self.path)

    def getting_path(self):
            print(self.path)
            print(self.device)
            return self.path

demo.robot
===============
*** Settings ***
*** Variables ***
${path}    c:
${device}    samsung
${path1}    D:
${device1}    samsung11
*** Test Cases ***
Test
    Test_python_class
*** Keywords ***

Test_python_class
     Import Library      demo.Sample    ${path1}    ${device1}    
     ${result} =     demo.sample.getting_path
     log     ${result}
     Import Library      demo.Sample    ${path}    ${device}
     ${result1} =     demo.sample.getting_path
     log     ${result1}

它没有创建第二个对象。${result} 和 {result1} 打印相同的值。

我可以通过使用带有两个值的 WITH 名称的以下语法来实现这一点。并使用 WITH NAME 进行如下调用

Import Library      demo.Sample    ${path1}    ${device1}    With Name     c1
     ${result} =     c1.getting_path
     log     ${result}
     Import Library      demo.Sample    ${path}    ${device}   With Name     c2
     ${result1} =     c2.getting_path
     log     ${result1}

但是这个解决方案并不是最优的。如果我需要创建 10 个不同 value 的对象,我需要在这里使用 10 个 import 语句。

感谢是否有人可以提供有关最佳解决方案的任何输入,我可以将这些步骤定义为机器人函数/关键字,它将为构造函数获取这些参数并返回对象句柄,以便我们可以使用不同的对象调用类方法。

4

2 回答 2

0

您可以进行以下更改以使其正常工作

  1. 用文件名重命名你的 python 类。

演示.py

class demo:

def __init__(self,path,device):
        self.device=device
        self.path = path
        print(self.device)
        print(self.path)

def getting_path(self,path2,device2):
    self.path=path2
    self.device=device2
    print(self.path)
    print(self.device)
    return self.path
  1. 在设置部分导入类

  2. 使用 [模板] 功能使您的 KW 数据驱动

演示机器人

    *** Settings ***
Library     demo.py     path=${path}   device=${device}        WITH NAME    mylib
*** Variables ***
${path}    f:
${device}    samsung

*** Test Cases ***
Test
    [Template]  Test_python_class
    c:  samsung
    d:  HTC
    e:  Yahoo
    f:  Sony

*** Keywords ***
Test_python_class
    [Arguments]  ${arg1}  ${arg2}
     ${result} =     mylib.getting path  ${arg1}    ${arg2}
     log to console     ${result}

输出

==============================================================================
Test                                                                  c:
.d:
.e:
.f:
Test                                                                  | PASS |
------------------------------------------------------------------------------

您可以继续增加参数的数量,如测试用例所示。

于 2020-04-27T09:57:58.423 回答
0

机器人框架并不是真正设计来以这种方式创建对象的。当您使用import library关键字(或Library设置)时,机器人需要一个关键字库,而不是标准的 Python 模块。

相反,我建议创建一个适当的关键字库,其中包含用于创建对象的关键字。

例如,从如下所示的SampleLibrary.py文件开始:

# SampleLibrary.py
from demo import Sample

class SampleLibrary:
    def get_sample(self, path, device):
        return Sample(path, device)

然后,在您的测试中,您将执行以下操作:

*** Settings ***
Library  SampleLibrary

*** Test Cases ***
Example
    ${sample1}=  get sample  ${path}   ${device}
    Call method  ${sample1}  getting_path

    ${sample2}=  get sample  ${path1}  ${device1}
    Call method  ${sample2}  getting_path
于 2020-04-27T13:33:29.087 回答