[英]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.