繁体   English   中英

Robot Framework关键字和继承

[英]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以始终引用超类,因为该方法仅在那里定义,并且仅由子类继承?

来自Robot Framework用户指南

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM