Skip to content
Tutorials Center
My learning

Topics

Lesson 16 of 36 ยท Libraries and Resources

Resource Files and Imports

Share keywords and variables across suites with .resource files. Learn import paths, ${CURDIR}, nested resources, name prefixes and a layered keyword architecture.

As soon as you have more than one suite, you will want to reuse keywords and variables. Copying them is a trap: every change must be made in many places. A resource file collects shared keywords and variables in one file that any suite can import. This lesson shows how to create and import them, how paths are resolved, and how to arrange keywords in layers so that tests stay stable when the application changes.

What a resource file is

A resource file has the extension .resource (the recommended one; .robot and .txt also work). It may contain *** Settings ***, *** Variables *** and *** Keywords *** sections, but no test cases.

resources/shop.resource:

ROBOTFRAMEWORK
*** Settings ***
Documentation    Shared keywords for the shop tests.
Library          Collections
Resource         common.resource

*** Variables ***
${SHOP_URL}      https://shop.example.org
${DEFAULT_QTY}   ${1}

*** Keywords ***
Open Shop
    Log    Opening ${SHOP_URL}

Add Book To Cart
    [Arguments]    ${title}    ${quantity}=${DEFAULT_QTY}
    Log    Adding ${quantity} x ${title}

A suite that uses it:

ROBOTFRAMEWORK
*** Settings ***
Resource    ../resources/shop.resource

*** Test Cases ***
Buy a book
    Open Shop
    Add Book To Cart    Robot Basics    quantity=2
    Log    Shop URL was ${SHOP_URL}

The suite can use every keyword and variable from the resource, and also those that the resource itself imports (libraries, other resources).

Import paths

ROBOTFRAMEWORK
*** Settings ***
Resource    shop.resource                     # same folder as this file
Resource    ../resources/shop.resource        # relative to this file
Resource    ${CURDIR}/../resources/shop.resource
Resource    ${EXECDIR}/resources/shop.resource
Resource    pkg/shop.resource                 # also searched in the module search path
  • A relative path is first resolved from the importing file's directory, then through the Python module search path (--pythonpath, PYTHONPATH).
  • Use forward slashes in paths; they work on every operating system.
  • ${CURDIR} is the importing file's folder; ${EXECDIR} is where robot was started. Prefer paths relative to the file, so tests run from any working directory.
  • A resource imported by several files is loaded once per suite; importing the same resource twice in one file is harmless but pointless.

Variables and conflicts

Variables in a resource behave like suite variables of every importing suite. If a suite defines the same variable, the suite's Variables section wins (see Variable Scopes). Keyword name clashes between resources are resolved with a prefix, which is the file name without extension:

ROBOTFRAMEWORK
shop.Open Shop
common.Open Shop

A layered architecture

Large suites stay maintainable when keywords are layered by abstraction, and tests only talk to the top layer.

flowchart TB
    T["Test cases: business language<br/>Customer buys a book"] --> B["Business keywords: shop.resource<br/>Add Book To Cart, Pay With Card"]
    B --> P["Page or API keywords: pages/*.resource, api/*.resource<br/>Click Add Button, POST /orders"]
    P --> L["Libraries and Python helpers<br/>SeleniumLibrary, RequestsLibrary, MyHelpers.py"]

Each layer only calls the one below it. When a page changes, only the page layer is edited.

A project layout that follows this:

TEXT
project/
  tests/
    cart.robot
    checkout.robot
  resources/
    shop.resource          # business keywords
    pages/
      cart_page.resource   # UI details
    api/
      orders_api.resource
    common.resource        # setup, teardown, helpers
  libs/
    MyHelpers.py
  variables/
    staging.py
    production.py

Rules of thumb:

  1. Tests contain business-level keywords and almost no locators or URLs.
  2. Each resource file has one purpose and a documentation line.
  3. Resource files import what they need; tests import only the top-level resources.
  4. Avoid circular imports (A imports B imports A). Robot would accept some of them but they confuse readers.

Initialization files for directories

A folder with __init__.robot creates a suite from the whole directory; its Settings section can import a resource and set a suite setup for all files below it. That is often the place to import common.resource once. See Test Tags, Setup and Teardown for how setups propagate.

Stand-in 1: loading a resource graph

The blocks below are not Robot Framework. This stand-in loads a virtual project: each file lists the resources it imports and the keywords it defines, and the loader gathers everything visible to a suite while ignoring repeated or circular imports.

Python Stand-in: resource import graph

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Stand-in 2: layers of keywords

A test written at the business level expands into lower levels. The stand-in prints the call tree for one test, which shows why changing a button locator touches only the lowest layer.

Python Stand-in: keyword layers

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Common mistakes

  • Test cases in a resource file. They are ignored or cause an error; resources hold keywords and variables only.
  • Absolute paths such as C:/projects/.... They break on another machine.
  • One giant common.resource with hundreds of unrelated keywords. Split by purpose.
  • Hidden dependencies. A keyword that silently needs a variable defined in another file is hard to reuse; pass values as arguments.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which sections may a .resource file contain?

    Resource files may have Settings, Variables and Keywords sections, but no test cases.
  2. 2. How is a relative resource path resolved first?

    Relative paths start at the importing file's folder; the module search path is tried afterwards.
  3. 3. How do you call a keyword Open Shop defined in shop.resource when another resource also has Open Shop?

    Prefix the keyword with the resource file name without its extension.
  4. 4. What is the goal of layering keywords?

    Tests call business keywords, which call page or API keywords, which call libraries.

Summary

  • A .resource file holds shared Settings, Variables and Keywords; import it with Resource.
  • Relative paths resolve from the importing file; prefer them to absolute paths.
  • Use resource.Keyword prefixes to resolve name clashes.
  • Layer keywords: tests, business keywords, page or API keywords, libraries.
  • __init__.robot can import common resources for a whole directory.

Next: Writing Python Keyword Libraries