10

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 Answers 2

13

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)
Sign up to request clarification or add additional context in comments.

1 Comment

EXECDIR was exactly what I needed!
11

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

1 Comment

Thank you, this will come in handy.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.