setup pythonpath before starting test suite

旧巷老猫 提交于 2020-01-01 19:13:49

问题


Currently I am setting pythonpath as pybot --pythonpath ~/Test_suite main.robot while running the tests.

I also see there is option Set Environment Variable PYTHONPATH ${CURDIR} to set through robot framework. But it doesn't run before main settings

*** Settings ***
Documentation    Suite description
Resource         settings.robot

And below is settings.robot file

*** Settings ***
Resource         keywords/keywords_test.robot
Library          tests.test_1.TestClass

How to setup the pythonpath before running the suite?


回答1:


You can't do what you want. The settings are all processed before any test or keyword is run. You can use the --pythonpath option from the command line, or set the environment variable PYTHONPATH before starting your test.




回答2:


You can do it by adding to sys.path in suite init. E.g. you can create __init__.robot file in tests directory with:

*** Settings ***
Suite Setup    Setup before all tests

*** Keywords ***
Setup before all tests
    evaluate    sys.path.append(os.path.join("path", "to", "library"))    modules=os, sys

Described in official docs: http://robotframework.org/robotframework/latest/RobotFrameworkUserGuide.html#configuring-sys-path-programmatically




回答3:


I'm not sure if this would work, but I think there actually might be a way to do what you're talking about. It's pretty jenky, so bear with me.

You can't really do it in Robot Framework, but if you expand your horizons to Python, there's a neat little exploit I've found. If you take a look in other posts how to create a custom Python Library for Robot framework, you'll notice that there's a required method called __init__ with parameters of (self). I believe that this is the very first code to run when the Library instance is created. Little-known fact, you can add parameters to the creation of the Library instance. In your case, ~/Test_suite would be the value that you would pass.

IN THEORY, because I haven't tried this, you could tell __init__(self, path_in) to run your BuiltIn keyword with the following code:

self.BuiltIn().set_environment_variable('PYTHONPATH', path_in)

Don't forget to use the following import statement at the top of the file.

from robot.libraries.BuiltIn import BuiltIn


来源:https://stackoverflow.com/questions/33573608/setup-pythonpath-before-starting-test-suite

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!