Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Robot Framework : Unable to get keywords from class with __eq__ method

If a python class contains a __eq__ method, robot framework is unable to get the keywords from the class (The tests run and pass if the __eq__ method is commented out). For example, if my Python class (implemented in TestClass.py) is

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

    def get_arg1(self):
        return self.arg1

    def get_arg2(self):
        return self.arg2

    def __eq__(self, other):
        return self.arg1 == other.arg1 and self.arg2 == other.arg2

and my robot file (TestClass.robot) is

*** Settings ***
Library    TestClass    1    2    WITH NAME    First_Lib

*** Variables ***

*** Test Cases ***
MyTest1
    ${result}=    First_Lib.get arg1
    Should be equal as integers    ${result}    1
MyTest2
    ${result}=    First_Lib.get arg2
    Should be equal as integers    ${result}    2

I see the following error message when running robot v3.0.2.

[ ERROR ] Error in file 'TestClass.robot': Getting keyword names from library 'TestClass' failed: AttributeError: type object 'object' has no attribute 'arg1'

I'd like to understand if this is an unsupported use of the robot framework and if so, is there a recommended solution to rewrite/modify the class under test to avoid this error.

By executing the robot framework code through the debugger, I see that the error is originating from the method _get_handler_method in the class _ClassLibrary (in the file testlibraries.py). Being a newbie with the robot framework, I'm not sure how to address this problem.

Any suggestions will be of great help !!

like image 632
Saravanan Avatar asked Dec 16 '25 13:12

Saravanan


1 Answers

Your __eq__ method is buggy. Your implementation assumes that an instance will only be compared to another instance, but it can be compared to anything. For exapmle, if you compare an instance to a string, your function will throw an error since a string does not have an arg1 attribute.

A simple fix is to check that the two objects are of the same type, in addition to checking their attributes:

def __eq__(self, other):
    return (isinstance(other, self.__class__) and
            self.arg1 == other.arg1 and
            self.arg2 == other.arg2)
like image 51
Bryan Oakley Avatar answered Dec 19 '25 03:12

Bryan Oakley



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!