使用Python简化为Robot Framework创建大型测试库的工具。
项目描述
Python库核心
使用Python简化为Robot Framework创建大型测试库的工具。Robot Framework的混合和动态库API比静态库API提供了更多的灵活性,但它们也为库设置了要求,这些要求需要在库端实现。PythonLibCore通过提供更简单的接口和处理对Robot Framework库API的所有要求来简化这个问题。
代码已稳定,并被SeleniumLibrary和Browser library使用。项目支持Robot Framework的最新两个版本。
使用方法
可以使用两种方式使用PythonLibCore,要么通过HybridCore
,要么通过使用DynamicCore
。 HybridCore
提供了对混合库API的支持,而DynamicCore
提供了对动态库API的支持。请参考Robot Framework用户指南以选择正确的库API。
无论选择哪个库API,它们都有类似的要求。
- 库必须继承
HybridCore
或DynamicCore
。 - 库关键字必须使用Robot Framework的@keyword装饰器进行装饰。
- 在
HybridCore
或DynamicCore
的__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支持关键字名称和文档的翻译,但当前无法翻译参数名称、标签和类型。翻译提供为包含Json和Path对象的文件。翻译提供在HybridCore
或DynamicCore
的__init__
中的translation
参数。提供翻译文件是可选的,也不必为所有关键字提供翻译。
Json的键是方法名称,而不是实现关键字的关键字名称。键的值是包含两个键的Json对象:name
和doc
。name
键包含关键字翻译名称,doc
包含关键字的翻译文档。提供doc
和name
是可选的,例如,示例翻译json文件可以只为关键字名称或仅为文档提供翻译。但始终建议提供对name
和doc
的翻译。
库类文档和实例文档有特殊键,__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 的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | 2d695b2ea906f5815179643e29182466675e682d82c5fa9d1009edfae2f84b16 |
|
MD5 | 13924c9fbbf8b26259badcb3f22d79ee |
|
BLAKE2b-256 | 71895dc8c8186c897ee4b7d0b2631ebc90e679e8c8f04ea85505f96ad38aad64 |
robotframework_pythonlibcore-4.4.1-py2.py3-none-any.whl 的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | e0517129522aaa039eb2a28fd3d9720b7a0be0b90d0cbcb153a6c8016bb9e973 |
|
MD5 | 51853f6f316e9cc52b8266f184a5d8fe |
|
BLAKE2b-256 | ac6447d8403c7c0af89b46461640a2f67e49a5778062b8dd6eb3e128aa3c50cc |