Robot Framework:从 Python 库代码访问 Robot 的全局变量? [英] Robot Framework: access Robot's global variables from Python library code?

查看:57
本文介绍了Robot Framework:从 Python 库代码访问 Robot 的全局变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够从 Python 代码访问一些设置类型的全局变量.例如:

I have some settings-type global vars that I'd like to be able to access from Python code. For example:

pybot --variable RESULTS_PATH:/wherever/this/points test.txt

现在,我的模块 logger.py 文件需要知道 results_path 才能正确设置.

Now, my module logger.py file needs to know the results_path to set up properly.

我知道我可以用一个变量来初始化记录器,比如

I know that I can initialize the logger with a variable, like

***Settings***
Library  logger  ${RESULTS_PATH}

然后在记录器中我将被传递 results_path:

And then in logger I'll be passed results_path:

 def __init__(self, results_path):
   # Whatever

然而,对我来说这样做的问题是我想从 Python 代码和测试用例中访问和使用记录器.所以如果我这样设置,如果我想使用 Python 代码中的记录器,我会遇到同样的问题,需要 results_path 来正确初始化记录器.

However the problem with doing it this way for me is that I want to access and use the logger from both Python code and within test cases. So if I set it up this way, if I want to use the logger from Python code I run into the same problem of needing results_path to initialize the logger properly.

机器人框架库中是否有任何函数可以让我从 Python 代码中获取 ${RESULTS_PATH} 的值?做这样的事情的正确方法是什么?

Are there any functions in the robot framework library that would allow me to grab the value of ${RESULTS_PATH} from Python code? What is the proper way to do something like this?

现在,我对这个问题的解决方法是将 RESULTS_PATH 设置为环境变量.所以我有类似的东西:

Right now, my workaround for the issue is to set RESULTS_PATH as an environment variable. So I have something like:

运行方式:

RESULTS_PATH=/wherever/this/points pybot test.txt

文件test.txt:

***Settings***
Library  logger
...

文件logger.py:

results_path = os.environ["RESULTS_PATH"]
# Other set up stuff

推荐答案

您将需要使用 rf 的 BuiltIn 库,参考阅读此处的文档.这提供了内置于 Robot Framework 中的关键字,因此应该可靠地保持可用:

You will want to use rf's BuiltIn library, for reference read the documentation as found here. This provides the keywords that are built into Robot Framework and so should reliably stay usable:

from robot.libraries.BuiltIn import BuiltIn
results_path = BuiltIn().get_variable_value("${RESULTS_PATH}")

这篇关于Robot Framework:从 Python 库代码访问 Robot 的全局变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆