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

I created a tasks.py 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 tasks.py 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/int_variable.py

instead of:-

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

*** Settings ***
Variables     task.py
Variables     ${ROOT}/reference_libraries/resources/variables/int_variable.py

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

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

https://robotframework.org/robotframework/latest/RobotFrameworkUserGuide.html#built-in-variables

So

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

would be better

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

C:\Users\AhrenA\Documents\GitHub\yoyo-api-gateway-api-testing\functional_tests\partner

when what I want is:-

C:/Users/AhrenA/Documents/GitHub/yoyo-api-gateway-api-testing

which would give me absolute path :-

C:\Users\AhrenA\Documents\GitHub\yoyo-api-gateway-api-testing\reference_libraries\resources\variables\int_variable.py

Would this work then?

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

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