pankaj mishra
pankaj mishra

Reputation: 2615

Not able to call python function from robotframework

I have a Python class file 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

My sample.robot file is:

*** 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}

However, when I run the robot file, it gives the error:

Object 'sample_func' does not have method 'pankaj'

what wrong am I doing here?

Upvotes: 0

Views: 2035

Answers (2)

Bryan Oakley
Bryan Oakley

Reputation: 386342

The problem is that sample_func only accepts a single argument but you are passing two arguments. The self argument is automatically passed by python when the function is called, so your first argument is going to be assigned to arg1.

The solution is to either stop calling the keyword with two arguments, or add another argument to sample_func. Either of the following will work:

# 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}

Upvotes: 1

jozefow
jozefow

Reputation: 786

as error message says:

Keyword 'MakeMyClass' expected 2 arguments, got 0. 

MakeMyClass keyword in Test testcase was called without arguments.

Append 2 arguments after MakeMyClass (remember about separators after MakeMyClass and arguments - at least 2 spaces) in Test testcase:

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

Upvotes: 0

Related Questions