RobotFramework:pythonpath参数在使用api的run方法时不起作用

RobotFramework:pythonpath参数在使用api的run方法时不起作用,python,automation,robotframework,Python,Automation,Robotframework,我试图运行官方的RF计算器示例,通过提到pythonpath参数来指向.py文件(CalculatorLibrary.py) 当我从命令行将pythonpath传递给pybot时,它就工作了 [centos6 tmp]$ pybot -t "Push multiple buttons" -t "Push button" --pythonpath RobotDemo/ RobotDemo ======================================================

我试图运行官方的RF计算器示例,通过提到pythonpath参数来指向.py文件(CalculatorLibrary.py)

当我从命令行将pythonpath传递给pybot时,它就工作了

[centos6 tmp]$ pybot -t "Push multiple buttons" -t "Push button"  --pythonpath RobotDemo/ RobotDemo
==============================================================================
RobotDemo                                                                     
==============================================================================
RobotDemo.Keyword Driven :: Example test cases using the keyword-driven tes...
==============================================================================
Push button                                                           | PASS |
------------------------------------------------------------------------------
Push multiple buttons                                                 | PASS |
------------------------------------------------------------------------------
RobotDemo.Keyword Driven :: Example test cases using the keyword-d... | PASS |
2 critical tests, 2 passed, 0 failed
2 tests total, 2 passed, 0 failed
==============================================================================
RobotDemo                                                             | PASS |
2 critical tests, 2 passed, 0 failed
2 tests total, 2 passed, 0 failed
==============================================================================
Output:  /tmp/output.xml
Log:     /tmp/log.html
Report:  /tmp/report.html

但是,当我使用RunAPI执行此操作时,它不起作用

这是密码

import robot
abs_master_suite_path = "RobotDemo"
testcases_tobe_run_list = ["Push multiple buttons","Push button"]
this_path = ["RobotDemo"]
ret = robot.run(abs_master_suite_path,  test=testcases_tobe_run_list, pythonpath=this_path)

这是我得到的结果

[centos6 tmp]$ python run_pybot.py
==============================================================================
RobotDemo                                                                     
==============================================================================
[ ERROR ] Error in file '/tmp/RobotDemo/keyword_driven.txt': Importing test library 'CalculatorLibrary' failed: ImportError: No module named CalculatorLibrary
Traceback (most recent call last):
  None
PYTHONPATH:
  /usr/local/lib/python2.7/site-packages/robot/libraries
  /usr/local/lib/python2.7/site-packages
  /tmp
  /usr/local/lib/python2.7/site-packages/virtualenv-1.8.4-py2.7.egg
  /usr/local/lib/python2.7/site-packages/plumbum-1.1.0-py2.7.egg
  /usr/local/lib/python2.7/site-packages/six-1.2.0-py2.7.egg
  /usr/local/lib/python2.7/site-packages/pip-1.2.1-py2.7.egg
  /usr/local/lib/python2.7/site-packages/distribute-0.6.28-py2.7.egg
  /usr/local/lib/python2.7/site-packages/MySQL_python-1.2.4b4-py2.7-linux-i686.egg
  /usr/local/lib/python2.7/site-packages/Pinax-0.9a2-py2.7.egg
  /usr/local/lib/python2.7/site-packages/configparser-3.3.0r2-py2.7.egg
  /usr/local/lib/python27.zip
  /usr/local/lib/python2.7
  /usr/local/lib/python2.7/plat-linux2
  /usr/local/lib/python2.7/lib-tk
  /usr/local/lib/python2.7/lib-old
  /usr/local/lib/python2.7/lib-dynload
  .
RobotDemo.Keyword Driven :: Example test cases using the keyword-driven tes...
==============================================================================
Push button                                                           | FAIL |
No keyword with name 'Push button' found.
------------------------------------------------------------------------------
Push multiple buttons                                                 | FAIL |
No keyword with name 'Push button' found.
------------------------------------------------------------------------------
RobotDemo.Keyword Driven :: Example test cases using the keyword-d... | FAIL |
2 critical tests, 0 passed, 2 failed
2 tests total, 0 passed, 2 failed
==============================================================================
RobotDemo                                                             | FAIL |
2 critical tests, 0 passed, 2 failed
2 tests total, 0 passed, 2 failed
==============================================================================
Output:  /tmp/output.xml
Log:     /tmp/log.html
Report:  /tmp/report.html

RF无法找到给定的Python路径


在这种情况下,我是错了还是射频有问题?

“机器人”有两个功能来运行测试

run():运行测试的函数。

run_cli():使用命令行参数处理运行测试的函数。

在'Python27\Lib\site packages\robot\utils\application.py'中

    def execute_cli(self, cli_arguments):
    with self._logging():
        options, arguments = self._parse_arguments(cli_arguments)
        rc = self._execute(arguments, options)
    self._exit(rc)


    def execute(self, *arguments, **options):
    with self._logging():
        return self._execute(list(arguments), options)
'execute\u cli'调用'self.\u parse\u参数(cli\u参数),因此可以使用参数'pythonpath'。 “def parse_args”→ '_处理(特殊)选项" 方法“\u handle\u special\u options”调用

        if self._auto_pythonpath and opts.get('pythonpath'):
        sys.path = self._get_pythonpath(opts['pythonpath']) + sys.path
因此,您必须更新方法“def execute”

    def execute(self, *arguments, **options):
    with self._logging():
        if options['pythonpath']:
            sys.path=[options['pythonpath']]+sys.path
            del  options['pythonpath']       
        return self._execute(list(arguments), options)

最近也遇到了同样的问题。在点击robot.run API之前,您可以通过导入robot.pythonpath setter并调用add_path手动添加所需的pythonpath。但是,这可能会在程序中的其他地方导致意外的副作用,因此在测试运行完成后,您可能希望调用remove_path

例如:


通过RF用户组,我得到了官方确认,在使用pythonpath与robot.run时存在一些不一致,将在缺陷跟踪器中提出
from robot import pythonpathsetter
pythonpathsetter.add_path(additional_python_path)
robot.run(test_suite)
pythonpathsetter.remove_path(additional_python_path)