Setting a .py file variable to a robot framework environment variable for root path

I created a file in my project root with the following code to get the root path.

import pathlib
import robot.libraries.OperatingSystem

ROOT = pathlib.Path(__file__).parent.resolve().as_posix()

How do I assign the ROOT variable to a robot framework environment variable I can use in a test case to reference a variable file correctly with full path to end up with this:-

Variables ${ROOT}/reference_libraries/resources/variables/

instead of:-

Variables C:/Users/AhrenA/Documents/GitHub/yoyo-api-gateway-api-testing/reference_libraries/resources/variables/

*** Settings ***
Variables     ${ROOT}/reference_libraries/resources/variables/

should work, however, note that this is a builtin variable already

${EXECDIR} | An absolute path to the directory where test execution was started from.


*** Settings ***
Variables     ${EXECDIR}/reference_libraries/resources/variables/

would be better

That would give me the execution directory and not the project root directory so I get :-


when what I want is:-


which would give me absolute path :-


Would this work then?

*** Settings ***
Variables     ${EXECDIR}/../../reference_libraries/resources/variables/

Seems a lot simpler than including another python library.

So changed tact and just made sure to execute all tests from the project root folder, allowing me to use built in ${EXECDIR}

1 Like