简体   繁体   English

为用户定义的方法将测试库导入到 Robot Framework

[英]Importing test library to Robot Framework for user defined methods

I created Testclass.py as below:我创建了 Testclass.py 如下:

class Testclass(object):
    def testmethod(self):
        print "Hi"

And I accessed it from my login.robot file as我从我的login.robot文件访问它作为

Library   Testclass

and I called method testmethod from my Robot Framework suite file.我从我的机器人框架套件文件中调用了方法testmethod

But when I run through command line pybot login.robot I get import error:但是当我通过命令行pybot login.robot运行时,出现导入错误:

Error in file 'login.robot': Importing test library 'Testclass' failed: ImportError: No module named Testclass

If I don't define class and only define method it works.如果我不定义类而只定义方法,它就可以工作。

The problem is simply that robot cannot find your library.问题很简单,机器人找不到您的图书馆。 It only looks in places in your PYTHONPATH.它只在你的 PYTHONPATH 中查找。 So, one solution is to add the path to your library to your PYTHONPATH environment variable.因此,一种解决方案是将库的路径添加到 PYTHONPATH 环境变量中。

You can also use the --pythonpath option to pybot if you don't want to alter your PYTHONPATH.如果你不想改变你的 PYTHONPATH,你也可以使用 --pythonpath 选项来 pybot。

For example, assuming your file Testclass.py is in the folder ./robot/libraries , you can run your tests like this:例如,假设您的文件Testclass.py位于文件夹./robot/libraries ,您可以像这样运行测试:

pybot --pythonpath ./robot/libraries my_test_case.robot

For more information about this option, see the section Configuring where to search libraries and other extensions in the robot framework user guide.有关此选项的更多信息,请参阅机器人框架用户指南中的配置库和其他扩展的搜索位置部分。

You can also specify the file by path, if you want to hard-code the path of the file into your test case.如果要将文件的路径硬编码到测试用例中,还可以按路径指定文件。 If you do this, the class name inside the file must match the filename (eg: class Testclass in Testclass.py ).如果这样做,文件中的类名必须与文件名匹配(例如: class Testclass中的class Testclass Testclass.py )。

For example:例如:

*** Settings ***
| Library | robot/libraries/Testclass.py

This is covered in the robot framework user guide, in the section Specifying library to import .这在机器人框架用户指南的指定库导入部分中有所介绍

If your Testclass.py Library and .robot file are in the same folder use following:如果您的 Testclass.py 库和 .robot 文件在同一文件夹中,请使用以下内容:

Library  Testclass.py

Make sure that class and python file Name are the same.确保类和 python 文件名相同。

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

相关问题 在机器人框架中调用用户定义的测试库函数(带有参数)时发生typeError - typeError in calling user defined test library function ( with arguments ) in robot framework Robot Framework导入库实例不包含已定义的方法 - Robot Framework import library instance does not contain defined methods 在 Robot Framework 中导入自定义测试库 - Importing custom testing library in Robot Framework 在机器人框架中的 FOR 循环中导入库 - Importing Library inside FOR loop in Robot Framework 无法使用机器人框架中定义的关键字调用类方法 - Unable to call class methods with keyword defined in robot framework 在redshift UDF中导入用户定义的库 - importing user defined library in redshift UDF 如何使用python在机器人框架中导入和使用用户定义的类 - How to import and use user defined classes in robot framework with python 如何使用 python 在机器人框架中导入和使用用户定义的枚举类 - How to import and use user defined enum classes in robot framework with python 我可以将定义的机器人关键字放在 Robot Framework 的 Suite SetUp/Test Setup 下吗 - Can I put defined robot keywords under Suite SetUp/ Test Setup in Robot Framework `机器人框架中的测试用例超时,测试用例变量而不是在***变量***部分中定义的变量` - `Test Case Timeout in robot framework with test case variable instead of variable defined in *** Variables *** section`
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM