跳转到主要内容

使用Python简化为Robot Framework创建大型测试库的工具。

项目描述

Python库核心

使用Python简化为Robot Framework创建大型测试库的工具。Robot Framework的混合动态库API比静态库API提供了更多的灵活性,但它们也为库设置了要求,这些要求需要在库端实现。PythonLibCore通过提供更简单的接口和处理对Robot Framework库API的所有要求来简化这个问题。

代码已稳定,并被SeleniumLibraryBrowser library使用。项目支持Robot Framework的最新两个版本。

Version Actions Status License

使用方法

可以使用两种方式使用PythonLibCore,要么通过HybridCore,要么通过使用DynamicCoreHybridCore提供了对混合库API的支持,而DynamicCore提供了对动态库API的支持。请参考Robot Framework用户指南以选择正确的库API。

无论选择哪个库API,它们都有类似的要求。

  1. 库必须继承HybridCoreDynamicCore
  2. 库关键字必须使用Robot Framework的@keyword装饰器进行装饰。
  3. HybridCoreDynamicCore__init__中将实现关键字的类实例的列表提供给library_components参数。

还可以通过在方法上标记@keyword将关键字实现为库主类中的关键字。不需要在library_components参数中传递主库实例。

所有关键字,包括在主库外部类中实现的关键字,都作为方法在库实例中可用。这自动将库关键字发布为Python公共API中的方法。

以下示例演示了如何使用PythonLibCore与库一起使用。

示例

"""Main library."""

from robotlibcore import DynamicCore

from mystuff import Library1, Library2


class MyLibrary(DynamicCore):
    """General library documentation."""

    def __init__(self):
        libraries = [Library1(), Library2()]
        DynamicCore.__init__(self, libraries)

    @keyword
    def keyword_in_main(self):
        pass
"""Library components."""

from robotlibcore import keyword


class Library1(object):

    @keyword
    def example(self):
        """Keyword documentation."""
        pass

    @keyword
    def another_example(self, arg1, arg2='default'):
        pass

    def not_keyword(self):
        pass


class Library2(object):

    @keyword('Custom name')
    def this_name_is_not_used(self):
        pass

    @keyword(tags=['tag', 'another'])
    def tags(self):
        pass

插件API

可以使用PythonLibCore创建一个库的插件API。这允许通过外部Python类扩展库。插件可以在库导入时导入,例如通过在库的[__init__]{.title-ref}中定义参数来允许定义插件。可以通过用逗号分隔插件来定义多个插件。还可以通过用分号分隔参数为插件提供参数。

from robot.api.deco import keyword  # noqa F401

from robotlibcore import DynamicCore, PluginParser

from mystuff import Library1, Library2


class PluginLib(DynamicCore):

    def __init__(self, plugins):
        plugin_parser = PluginParser()
        libraries = [Library1(), Library2()]
        parsed_plugins = plugin_parser.parse_plugins(plugins)
        libraries.extend(parsed_plugins)
        DynamicCore.__init__(self, libraries)

当插件类看起来像这样

class MyPlugi:

    @keyword
    def plugin_keyword(self):
        return 123

那么在Robot Framework端可以像这样导入库

Library    ${CURDIR}/PluginLib.py    plugins=${CURDIR}/MyPlugin.py

翻译

PLC支持关键字名称和文档的翻译,但当前无法翻译参数名称、标签和类型。翻译提供为包含JsonPath对象的文件。翻译提供在HybridCoreDynamicCore__init__中的translation参数。提供翻译文件是可选的,也不必为所有关键字提供翻译。

Json的键是方法名称,而不是实现关键字的关键字名称。键的值是包含两个键的Json对象:namedocname键包含关键字翻译名称,doc包含关键字的翻译文档。提供docname是可选的,例如,示例翻译json文件可以只为关键字名称或仅为文档提供翻译。但始终建议提供对namedoc的翻译。

库类文档和实例文档有特殊键,__init__键将替换实例文档,__intro__将替换库类文档。

示例

如果有这样的库

from pathlib import Path

from robotlibcore import DynamicCore, keyword

class SmallLibrary(DynamicCore):
    """Library documentation."""

    def __init__(self, translation: Path):
        """__init__ documentation."""
        DynamicCore.__init__(self, [], translation.absolute())

    @keyword(tags=["tag1", "tag2"])
    def normal_keyword(self, arg: int, other: str) -> str:
        """I have doc

        Multiple lines.
        Other line.
        """
        data = f"{arg} {other}"
        print(data)
        return data

    def not_keyword(self, data: str) -> str:
        print(data)
        return data

    @keyword(name="This Is New Name", tags=["tag1", "tag2"])
    def name_changed(self, some: int, other: int) -> int:
        """This one too"""
        print(f"{some} {type(some)}, {other} {type(other)}")
        return some + other

当有翻译文件时

{
    "normal_keyword": {
        "name": "other_name",
        "doc": "This is new doc"
    },
    "name_changed": {
        "name": "name_changed_again",
        "doc": "This is also replaced.\n\nnew line."
    },
    "__init__": {
        "name": "__init__",
        "doc": "Replaces init docs with this one."
    },
    "__intro__": {
        "name": "__intro__",
        "doc": "New __intro__ documentation is here."
    },
}

然后 normal_keyword 被翻译为 other_name。同时,这个关键词文档也被翻译为 This is new doc。关键词 name_changed 被翻译为 name_changed_again 关键词,并且关键词文档也被翻译为 This is also replaced. 新的一行。 库类文档被翻译为 Replaces init docs with this one.,类文档被翻译为 New __intro__ documentation is here.

项目详情


下载文件

下载适用于您平台的文件。如果您不确定该选择哪一个,请了解更多关于 安装包 的信息。

源代码分发

robotframework-pythonlibcore-4.4.1.tar.gz (12.8 kB 查看哈希值)

上传时间 源代码

构建分发

robotframework_pythonlibcore-4.4.1-py2.py3-none-any.whl (12.5 kB 查看哈希值)

上传时间 Python 2 Python 3

由以下机构支持