英文:
How to share Robot Framework __init__.robot library initialization state with test cases?
问题
假设我有一个用Python实现的机器人库,它进行了一些状态初始化,就像这样:
```python
import uuid
class DemoLibrary:
ROBOT_LIBRARY_SCOPE = 'SUITE'
_my_uuid: str
def __init__(self) -> None:
self._my_uuid = str(uuid.uuid4())
def get_library_uuid(self) -> str:
return self._my_uuid
然后,在一个__init__.robot
文件中,我初始化了该库:
*** Settings ***
Library DemoLibrary
Suite Setup Set Up Suite
*** Keywords ***
Set Up Suite
${lib_uuid} Get Library Uuid
Set Global Variable ${GLOBAL_UUID} ${lib_uuid}
如果我然后尝试在由__init__.robot
文件定义的套件中的测试用例中使用该库,则库会被重新初始化:
a_test.robot
:
*** Settings ***
Library DemoLibrary
*** Test Cases ***
Setup Proof Of Concept
${lib_uuid} Get Library UUID
Should Be Equal ${GLOBAL_UUID} ${lib_uuid}
^ 这会失败,显示[FAIL] 9854fe14-47e5-4484-92c6-930a5cc5224c != d81635d0-0716-487f-9a22-1a450499652e
(显然,每次都会有唯一的uuid)
如果我记录来自Python库的self的对象id,我还可以看到它们在关键字调用之间发生更改。
我的目录结构如下:
demo
└── a_test/
├── __init__.robot
└── a_test.robot
libraries/
└── DemoLibrary.py
我使用robot --pythonpath=libraries a_test/
从demo中调用。
我尝试过各种组合的WITH NAME
、Import Library
和Get Library Instance
,但似乎没有一种方法可以工作。
是否有一种方法可以重用或共享在__init__.robot
中创建的库的实例?
<details>
<summary>英文:</summary>
Suppose I have a Robot library implemented in python that does some state initialization like this:
```python
import uuid
class DemoLibrary:
ROBOT_LIBRARY_SCOPE = 'SUITE'
_my_uuid: str
def __init__(self) -> None:
self._my_uuid = str(uuid.uuid4())
def get_library_uuid(self) -> str:
return self._my_uuid
And within an __init__.robot
file I initialize that library:
*** Settings ***
Library DemoLibrary
Suite Setup Set Up Suite
*** Keywords ***
Set Up Suite
${lib_uuid} Get Library Uuid
Set Global Variable ${GLOBAL_UUID} ${lib_uuid}
If I then try to use that library in a test case in the suite defined by the __init__.robot
file the library is re-initialized:
a_test.robot
:
*** Settings ***
Library DemoLibrary
*** Test Cases ***
Setup Proof Of Concept
${lib_uuid} Get Library UUID
Should Be Equal ${GLOBAL_UUID} ${lib_uuid}
^ This fails with [FAIL] 9854fe14-47e5-4484-92c6-930a5cc5224c != d81635d0-0716-487f-9a22-1a450499652e
(obviously, with unique uuids each time)
If I log the object ids of self from the python library I can also see that they change between the keyword invocations.
My directory structure looks like this:
demo
└── a_test/
├── __init__.robot
└── a_test.robot
libraries/
└── DemoLibrary.py
I'm invoking the from demo with robot --pythonpath=libraries a_test/
.
I've tried various combinations of WITH NAME
, Import Library
and Get Library Instance
but none of them seemed to work.
Is there a way reuse or share the instance of the library created in __init__.robot
?
答案1
得分: 3
短答案是,套件初始化文件不会向套件的其他文件提供导入或变量,所以您在这里尝试实现的目标将不起作用。
长答案是,您可以通过首先在ROBOT_LIBRARY_SCOPE
行上将库定义为'GLOBAL'
来在套件之间共享库状态,以防止为每个测试套件创建新实例。这将使得在同一会话中运行的每个测试套件都将使用相同的库实例。但请注意,如果您的库需要参数,提供新的参数将始终强制创建一个新实例。关于库作用域的更多信息,请参阅Robot Framework用户指南。
在测试套件之间共享属性时,通常最好使用资源文件。这在您的共享库需要参数时会有所帮助 - 如果您为库导入提供不同的参数值,它将始终创建一个新实例。使用资源文件也通常是一个良好的实践。
要在导入中使用资源文件,向项目中添加一个新文件resources.robot
,然后在那里导入库。在稍后的测试套件文件中使用资源文件时,资源文件只会导入库一次。
英文:
Short answer is that suite initialization files do not provide imports or variables down to rest of the suite files, so what you are trying to achieve here will not work.
Long answer is that you can share library state between suites by first defining the library as 'GLOBAL'
on the ROBOT_LIBRARY_SCOPE
line to prevent a new instance being created for each test suite you run. This will make it so that each test suite run in the same session will use the same library instance. Notice however that if your library requires arguments, giving new arguments will force a new instance to be created always. There's more info about library scope in the Robot Framework User Guide.
When sharing properties between test suites it is usually good idea to use a resource file. This helps when your shared library requires arguments - If you provide different argument value to the library import it will always create a new instance. Using resource files is also generally a good practice.
To use a resource file for the import add a new file resources.robot
to your project and import the library there. When using the resource file in the test suite files later, the resource file only imports the library once.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论