简体   繁体   中英

Unable to call class methods with keyword defined in robot framework

Hi I am trying to create my own keywords and call it from robot scripts :

Below is my sample code :

util.py

from robot.api.deco import keyword

@keyword('Add Num')
def add(a,b):
    print "Simple method"
    return int(a)+int(b)
class Geo :
    @keyword('Class Add Num')
    def addd(self,a,b):
        print "Inside class method "
        return int(a)+int(b)

Sample.robot

*** Settings ***
Library   util.py

*** Test Cases ***
TC_01
    [Documentation]  sample test
    [Tags]  Sample
    Add Num  10  20
    Class Add Num  10  2

Which I am running using pybot command - pybot sample.robot

Add Num keyword is works fine , but Class Add Num gives an error :

No keyword with name 'Class Add Num' found.

I have tried looking into stackoverflow and official robot framework docs , but could find any relevant help .

How to resolve this error , Or I need to implement it in a different way ??

Robot will not automatically instantiate in classes in your libraries, except in one special case which is when the class name is the same as the base part of the filename.

The proper way to write a library is to either use a single class with the same name as a filename, or functions, but not both. If you want to use both, it is up to you to create instances of your class and expose the keywords.

The third way is to use the dynamic library api in which you create special functions to get the list of available keywords ( get_keyword_names ), and to execute a keyword ( run_keyword ). This is all documented in the user guide.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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