10
votes

I want to run tests in Robot Framework.

I would also like the following kind of directory structure for the robot framework tests:

  • Root directory
    • Libraries
      • Library.py
    • Resource Files
      • Resource.txt
    • Tests
      • test_1.txt
      • test_2.txt

Or something along those lines. However, I do not know how to write my tests so they can access my library and resource files. For example, how to import Libraries\Library.py from Tests\test_1.txt.

What would be the best way to approach this?
Is there a syntax for acessing files in the parent directory?
Should I import the resource and library files in every test file, or is there a way to do it only once?

2

2 Answers

12
votes

Robot automatically defines an ${EXECDIR} variable that we're using in place of ${ROOT} from Bryan's answer.

Pros:

  • System-independent

Cons:

  • May depend on how you invoke PyBot (working dir in command prompt, or which folder you open in RIDE)
11
votes

Using relative imports

Robot supports relative imports. You can use .. to represent the parent of a directory. In your example you would do it this way:

*** Settings ***
| Resource | ../Resource Files/Resource.txt
| Library  | ../Libraries/Library.py

Defining the root in a variable

You can use variables in your settings table, so you could define a variable that points to the root of your repository. You would use this variable for all of your imports. For example:

*** Settings ***
| Resource | ${ROOT}/Resource Files/Resource.txt
| Library  | ${ROOT}/Libraries/Library.py

You can set this variable on the command line with the --variable option:

$ pybot --variable ROOT /path/to/root tests