user2661518
user2661518

Reputation: 2755

setup pythonpath before starting test suite

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?

Upvotes: 1

Views: 23579

Answers (3)

soyacz
soyacz

Reputation: 457

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

Upvotes: 3

Brandon Olson
Brandon Olson

Reputation: 639

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

Upvotes: 0

Bryan Oakley
Bryan Oakley

Reputation: 386342

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.

Upvotes: 3

Related Questions