如何导入python模块并在Robot Ride中公开方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16692593/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to import python modules and expose the methods in Robot Ride
提问by Karthick
I have my individual python modules which has many methods to it.
我有我的个人 python 模块,它有很多方法。
For Example:
例如:
ReusableModule.py has
ReusableModule.py 有
def play_button():
print 'Does play Operation'
def download_music():
print 'Does Download Operation'
I want to use the methods as Keyword in RobotFramework Ride. How to make the methods visible from RIDE?
我想在 RobotFramework Ride 中使用这些方法作为关键字。如何使 RIDE 中的方法可见?
PS: Edited class name so ROBOT Framework can Identify
PS:编辑类名以便机器人框架可以识别
When I execute get the following error from Ride: 20130524 01:32:09.254 : FAIL : No keyword with name 'play_button' found.
当我执行时,从 Ride 中得到以下错误:20130524 01:32:09.254 : FAIL : No keyword with name 'play_button' found.
采纳答案by theheadofabroom
The key is in your naming convention - functions and methods in python should be lowercase and the words should be separated by underscores. If you follow that convention, robot framework will pick up these keywords and allow you to use them in your tests, however in the tests the words should be separated by spaces, and are case insensitive. I believe that of you read the documentation there are ways of exposing keywords without following the standard naming convention, but I would urge you top follow convention, especially of anyone else might have to read your code. I would recommend reading PEP-8 as it gives the main style guidelines.
关键在于你的命名约定——python 中的函数和方法应该是小写的,单词应该用下划线分隔。如果您遵循该约定,机器人框架将选取这些关键字并允许您在测试中使用它们,但是在测试中,单词应该用空格分隔,并且不区分大小写。我相信阅读文档的人有很多方法可以在不遵循标准命名约定的情况下公开关键字,但我强烈建议您遵循约定,尤其是其他人可能需要阅读您的代码。我建议阅读 PEP-8,因为它提供了主要的风格指南。
Further Explanation
进一步说明
Assuming your have the following ReusableModule.py:
假设您有以下内容ReusableModule.py:
class ReusableModule(object):
def play_button(self, args):
print "Pressed Play"
You would import like so:
你会像这样导入:
Library ReusableModule
and then run the keyword in your test case as Play Button
然后在您的测试用例中运行关键字 Play Button
As long as ReusableModule.pyis in your path when you run the test you should be fine - this means that it is either in your current directory, or $PYTHONPATH - you can check this by running:
只要ReusableModule.py在您运行测试时在您的路径中,您就应该没问题-这意味着它在您的当前目录或 $PYTHONPATH 中-您可以通过运行来检查:
python -c "from ReusableModule import ReusableModule"
from the command line - if this works you should be able to run your test
从命令行 - 如果这有效,你应该能够运行你的测试
回答by G.Wolf
First of all, to use your module/library in robotframework PYTHONPATH has to contain path to your module. This is no different as is with Python. To make your module known to robotframewotk, make sure you use
首先,要在机器人框架中使用模块/库,PYTHONPATH 必须包含模块的路径。这与 Python 没有什么不同。要让 robotsframewotk 知道您的模块,请确保您使用
Library ReusableModule
in Settings section of Test Suite.
在测试套件的设置部分。
Next, when running tests with pybot on Linux you could do something like this
接下来,在 Linux 上使用 pybot 运行测试时,您可以执行以下操作
$ export PYTHONPATH=/directory/contsaining/your/module
$ pybot <options>
With RIDE, you have to modify RIDE settings and also add path to your module. If that is done correctly you should be able to run tests with RIDE and also will have your keywords show up in RIDE completion.
使用 RIDE,您必须修改 RIDE 设置并添加模块的路径。如果正确完成,您应该能够使用 RIDE 运行测试,并且您的关键字也会显示在 RIDE 完成中。

