20

Assume I have a class in python:

class TestClass(object):
    def __init__(self, arg1, arg2):
        self.arg1 = arg1
        self.arg2 = arg2

    def print_args(self):
        print arg1, arg2

I want to use robotframework to implement my tests scenarios. I want to make an instance from above class and call its methods. How to do that? I know how to import the lib; it should be like this:

Library   TestClass

I don't know how to initialize an object from this class and call class methods via this object. If I wanted to implement it with python, I would write some piece of code like this:

import TestClass
test = TestClass('ARG1', 'ARG2')
test.print_args()

Now, I want to know how to write this in robotframework. Any help?

Laurent Bristiel
  • 6,819
  • 34
  • 52
Zeinab Abbasimazar
  • 9,835
  • 23
  • 82
  • 131

2 Answers2

24

To import the library with arguments, just add them after the library name:

Library  TestClass  ARG1  ARG2

So the "import" and the instantiation are done in one shot. Now, the thing that can be tricky is to understand the scope of your instance. This is well explained in the User Guide section "Test Library Scope":

A new instance is created for every test case. [...] This is the default.

Note that if you want to import the same library several times with different arguments, and hence have difference instances of your classes, you will have to name them on import:

Library  TestClass  ARG1  ARG2  WITH NAME  First_lib
Library  TestClass  ARG3  ARG4  WITH NAME  Second_lib

And then in your tests, you have to prefix the keywords:

*** Test Cases ***
MyTest
    First_lib.mykeyword  foo  bar
    Second_lib.mykeyword  john  doe

This is explained in this section of the User Guide.

Laurent Bristiel
  • 6,819
  • 34
  • 52
  • 1
    Thank you for the response. I really understand how to use my class. But as I read the docs, I find out that I can only have one instance from every class; because the framework does not save any ID or name for instances and I just call methods of class without referring to anything, Did I get it right? – Zeinab Abbasimazar Dec 24 '14 at 06:29
  • 2
    you can have multiple instances in fact. I edited my answer to address this point. – Laurent Bristiel Dec 24 '14 at 08:57
  • Thanks for your comprehensive answer. – Zeinab Abbasimazar Dec 24 '14 at 10:00
6

I have been able to instantiate python classes on-demand (i.e. not just hard-coded args as via the Library technique).

I used a helper method to create the class. I was unable to get the Robot script to call the class constructor directly, however it is able to call functions in Python, so we can create a class or namedtuple by providing a function-based interface:

File: resource_MakeMyClass.robot

*** Settings ***
Library             myClass

*** Keywords ***
_MakeMyClass
    [Arguments]    ${arg1}    ${arg2}
    ${result} =    makeMyClass    ${arg1}    ${arg2}
    [Return]       ${result}

File: myClass.py

class MyClass(object):
    def __init__(self, arg1, arg2):
        self.arg1 = arg1
        self.arg2 = arg2

def makeMyClass(arg1, arg2):
    return MyClass(arg1, arg2)
Zeinab Abbasimazar
  • 9,835
  • 23
  • 82
  • 131
user3588820
  • 61
  • 1
  • 2
  • 1
    How do you use it in a test case? I tried `${c} = _MakeMyClass z m` and it gave me `Maximum limit of started keywords exceeded.` error. Seems it falls into an infinite loop. – Zeinab Abbasimazar Apr 28 '20 at 09:15