简体   繁体   English

在 Robot Framework 中导入自定义测试库

[英]Importing custom testing library in Robot Framework

I am writing a custom testing library for Robot Framework in Python and I want to import it like this:我正在为Python中的Robot Framework编写一个自定义测试库,我想像这样导入它:

Library         CustomLibrary

I put the folder containing the source code on the PYTHONPATH but I am still getting the error: Importing test library 'CustomLibrary' failed: ImportError: No module named CustomLibrary我将包含源代码的文件夹放在PYTHONPATH上,但仍然出现错误: Importing test library 'CustomLibrary' failed: ImportError: No module named CustomLibrary

The CustomLibrary class is defined in the __init__.py file, like in the AppiumLibrary like this: CustomLibrary class 在__init__.py文件中定义,就像在AppiumLibrary中这样:

from CustomLibrary.keywords import *

class CustomLibrary(_CustomKeywords):
    ROBOT_LIBRARY_SCOPE = 'GLOBAL'

How can I solve this problem, so I can import it in Robot Framework?我该如何解决这个问题,以便将其导入 Robot Framework? I want to keep the class definition inside the init file.我想将 class 定义保留在 init 文件中。

You need to make sure that the folder containing CustomLibrary is on PYTHONPATH.您需要确保包含CustomLibrary的文件夹位于 PYTHONPATH 上。

For example, the following works as expected for me:例如,以下对我来说按预期工作:

  1. create a folder in /tmp named CustomLibrary在 /tmp 中创建一个名为 CustomLibrary 的文件夹
  2. create a file named /tmp/CustomLibrary/__init__.py创建一个名为 /tmp/CustomLibrary/__init__.py 的文件
  3. define a class named CustomLibrary in /tmp/CustomLibrary/__init__.py在 /tmp/CustomLibrary/__init__.py 中定义一个名为 CustomLibrary 的 class
  4. define a method in the CustomLibrary class在 CustomLibrary class 中定义一个方法
  5. import the library in a test with Library CustomLibrary使用Library CustomLibrary在测试中导入库
  6. add /tmp to PYTHONPATH and run robot.将 /tmp 添加到 PYTHONPATH 并运行机器人。 For example, robot --pythonpath /tmp example.robot例如, robot --pythonpath /tmp example.robot

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM