簡體   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