Robot Framework关键字和继承

时间:2015-09-08 19:00:13

标签: python inheritance robotframework

我有一个关键字库。我有一些类和子类,但我有继承问题和双重定义的关键字。例如:

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

2 个答案:

答案 0 :(得分:0)

来自Robot Framework User Guide

registered_var.results[x].item.results[y].item.results[z].item...

您是否考虑过使用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

答案 1 :(得分:0)

我认为最好的解决方案是将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__之类的异国情调来解决此问题,但这可能不值得。