[英]Robot Framework keywords and Inheritance
我有一个关键字库。 我有一些类和子类,但是在继承和关键字被双重定义时遇到了问题。 例如:
MyLib.py
class Class1:
def __init__(self):
pass
def do_something_generic(self):
#do stuff that is generic to all subclasses
pass
class Subclass1(Class1):
def __init__(self):
pass
def do_something_specific_to_subclass1(self):
#something specific
pass
class Subclass2(Class1):
def __init__(self):
pass
def do_something_specific_to_subclass2(self):
#something specific
pass
特定的关键字可以正常工作,但是当我尝试调用Do Something Generic
我找到了Multiple keywords with name 'Do Something Generic' found
。 我可以使用MyLib.Class1.Do Something Generic
完全限定库名,但是有什么方法可以定义“ Do Something Generic
以始终引用超类,因为该方法仅在那里定义,并且仅由子类继承?
When the static library API is used, Robot Framework uses reflection to find out what public methods the library class or module implements.
It will exclude all methods starting with an underscore,
and with Java libraries also methods that are implemented only in java.lang.Object are ignored.
All the methods that are not ignored are considered keywords.
您是否考虑过通过_do_something_generic
函数添加辅助基类? 您可以将其从__all__
列表中排除。 然后使用继承公开Class1
基类的关键字。
MyLibrary.py:
__all__ = ['Class1', 'Subclass1', 'Subclass2']
class BaseClass:
def _do_something_generic(self):
pass
class Class1(BaseClass):
def __init__(self):
pass
def do_something_generic(self):
return self._do_something_generic()
class Subclass1(BaseClass):
def __init__(self):
pass
def do_something_specific_to_subclass1(self):
a = self._do_something_generic()
return (a, 3)
class Subclass2(BaseClass):
def __init__(self):
pass
def do_something_specific_to_subclass2(self):
#something specific
pass
我认为最好的解决方案是将do_something_generic
移至一个单独的类,以便您的基类仅具有辅助函数,而没有公共关键字:
class Class1:
def __init__(self):
pass
class Subclass0(Class1):
def do_something_generic(self):
#do stuff that is generic to all subclasses
pass
虽然可能可以通过使用__slots__
或__getattr__
或修改self.__dict__
之类的奇特方法来解决此问题,但这样做并不值得。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.