Robot Framework: Cannot get keywords from class using __eq__ method
If the python class contains the __ eq __ method , the robot framework cannot get keywords from the class (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 )
*** 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
When starting robot v3.0.2 , the following error message appears:
[ ERROR ] Error in file 'TestClass.robot': Getting keyword names from library 'TestClass' failed: AttributeError: type object 'object' has no attribute 'arg1'
I would 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.
While executing the robot framework code through the debugger, I can see that the error comes from the _get_handler_method method in the _ClassLibrary class (in the testlibraries.py file ). As a newbie with a robot platform, I'm not sure how to fix this issue.
Any suggestions would be very helpful.
source to share
Your method __eq__
is wrong. Your implementation assumes that an instance will only compare to another instance, but it can be compared to anything. For exapmle, if you are comparing an instance to a string, your function throws an error as the string has no attribute arg1
.
A simple solution 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)
source to share