【问题标题】:Not able to call python function from robotframework无法从机器人框架调用 python 函数
【发布时间】:2017-10-11 19:18:50
【问题描述】:

我有一个 Python 类文件Myclass.py

class Myclass(object):
    def __init__(self,age,marks,city):
        self.age=age
        self.marks=marks
        self.city=city


    def sample_func(self ,arg1):
        self.arg1=arg1
        return self.age,self.marks,self.city

我的 sample.robot 文件是:

*** Settings ***
Library      Myclass.py    ${age}    ${marks}    ${city}

*** Variables ***
${arg1}    pankaj
${arg2}    Mishra
${age}    35
${marks}    26
${city}    noida

*** Test Cases ***
Test
    Test_MakeMyClass    ${arg1}    ${arg2}

*** Keywords ***
Test_MakeMyClass
    [Arguments]    ${arg1}    ${arg2}
    #Below command is working
    #${result} =    Myclass.sample_func    ${arg1}

    #$This one is throwing error 
    ${result} =     Call Method     Myclass.sample_func    ${arg1}    ${arg2}
   [Return]       ${result}

但是,当我运行机器人文件时,它给出了错误:

Object 'sample_func' does not have method 'pankaj'

我在这里做错了什么?

【问题讨论】:

  • 为什么要创建一个关键字来创建库的新实例?这不是机器人库的工作方式。
  • @Bryan Oakley,我现在已经编辑了我的代码..现在无法理解问题是什么
  • 上面的代码没有给出错误'对象'sample_func'没有你声称的'pankaj'方法。请确保您发布的代码是给出您所说的错误的实际代码。
  • @Bryan Oakley..i 能够通过命令 ${result} = Myclass.sample_func ${arg1} 获得结果传递 ..我在这里复制粘贴我的机器人文件内容

标签: python robotframework


【解决方案1】:

问题是sample_func 只接受一个参数,但您传递了两个参数。 self 参数在调用函数时由 python 自动传递,因此您的第一个参数将分配给 arg1

解决方案是停止使用两个参数调用关键字,或者向sample_func 添加另一个参数。以下任一方法都可以:

# Myclass.py
class Myclass(object):
    ...
    def sample_func(self, arg1):
        ...

# sample.robot
...
sample_func  ${arg1}

# Myclass.py
class Myclass(object):
    ...
    def sample_func(self, arg1, arg2):
        ...

# sample.robot
...
sample_func  ${arg1}  ${arg2}

【讨论】:

    【解决方案2】:

    如错误消息所述:

    Keyword 'MakeMyClass' expected 2 arguments, got 0. 
    

    MakeMyClass 测试用例中的关键字在没有参数的情况下被调用。

    在测试用例中的 MakeMyClass 之后附加 2 个参数(记住 MakeMyClass 和参数之后的分隔符 - 至少 2 个空格):

    *** Test Cases ***
    Test
        MakeMyClass  something1  someghing2
    

    【讨论】:

    • @jozefow..please see the edits..我现在收到错误对象'makeMyClass'没有方法'"pankaj"'。
    • 您是否需要将类的实例返回给 Robot 或者您的库只是一个任意示例? * 将 def makeMyClass(arg1, arg2): 更改为 def makeMyClass(self,arg1, arg2): * 尝试使用唯一的关键字名称,在库和机器人套件中,MakeMyClass 是有效的,但会导致实际测试套件中的错误
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-11-02
    • 1970-01-01
    • 2017-11-22
    • 2022-12-28
    • 2020-06-18
    • 1970-01-01
    • 2020-10-11
    相关资源
    最近更新 更多