无法从robotframework调用python函数

时间:2017-10-09 13:02:40

标签: python robotframework

我有一个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'
我在这里做错了什么?

2 个答案:

答案 0 :(得分:1)

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

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

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

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

OR

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

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

答案 1 :(得分:0)

正如错误消息所示:

Keyword 'MakeMyClass' expected 2 arguments, got 0. 
测试testcase中的

MakeMyClass 关键字是在没有参数的情况下调用的。

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

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