Robot Framework:无法使用

编程入门 行业动态 更新时间:2024-10-25 16:29:49
Robot Framework:无法使用__eq__方法从类中获取关键字(Robot Framework : Unable to get keywords from class with __eq__ method)

如果python类包含__eq__方法,则机器人框架无法从类中获取关键字(如果__eq__方法被注释掉,测试将运行并通过)。 例如,如果我的Python类(在TestClass.py中实现)是

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

和我的机器人文件( 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

运行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'

我想了解这是否是对机器人框架的不支持使用,如果是这样,是否有建议的解决方案来重写/修改被测试的类以避免此错误。

通过调试器执行机器人框架代码,我发现错误来自类_ClassLibrary中的方法_get_handler_method (在文件testlibraries.py中 )。 作为机器人框架的新手,我不知道如何解决这个问题。

任何建议都会有很大的帮助!!

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 !!

最满意答案

你的__eq__方法有问题。 您的实现假定实例仅与另一个实例进行比较,但可以将其与任何实例进行比较。 例如,如果将实例与字符串进行比较,则函数将抛出错误,因为字符串没有arg1属性。

一个简单的解决方法是检查两个对象是否属于同一类型,另外还要检查它们的属性:

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

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)

更多推荐

本文发布于:2023-07-30 00:04:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1320822.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Robot   Framework

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!