3

Engine.py will import several classes as self object

Engine.py

from api import api
from cloud import cloud
class Engine(object):
    def __init__(self, env):
        session = dict()
        self.api = api.API(session)
        self.cloud= cloud.CLOUD(session)

api.py

class API(object):
    def __init__(self, session):
        self.session = session

    def api_keyword(self):
        return SOMETHING

My question is :

How can I use the keyword under api.py and cloud.py and ONLY import Engine.py into robot file

test.robot

*** Settings ***
Library         Engine.py  ${env}


*** Test Cases ***
python class test
    [Tags]    class
    Engine.api.api_keyword

And I got error message:

No keyword with name 'Engine.api.api_keyword' found.

2
  • There isn't much you can do at the robot test level. Are you able to change Engine.py? Commented Oct 10, 2018 at 22:08
  • @BryanOakley yes, I can change Engine.py. And I need to let API and XCLOUD share self.session Commented Oct 11, 2018 at 4:15

1 Answer 1

1

Robot Framework maps only class methods to keywords; your class Engine does not expose any methods from api and cloud - it probably uses them internally, but doesn't define any as its own.
So here's your first solution - create wrapper methods for all you need in the cases:

def an_api_method(self):
    self.api.something()

And now you'll have the An API Method keyword at your disposal in the cases.


Solution two - make your class inherit the other two:

class Engine(api, cloud):

, and your cases will have access to all their public methods.
This one is more involving - you'll have to call their constructors (with super()), and if you maintain a state in your class, you'll have to accommodate for that. I.e. more drastic code changes are needed.


The third solution doesn't require any changes to the Enhine code - but, disclaimer: I don't know will it work :) (I'm not at a computer).
It consists of two calls - first to use Get Library Instance to get the object of your imported library (from the Builtin library), and then - Call Method:

${ref}=     Get Library Instance    Engine
Call Method     $ref.api    api_keyword
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.