Resource files¶
Resource files are typically created using the plain text format, but also reStructuredText, Markdown and JSON resource files are supported.
Taking resource files into use¶
Resource files are imported using the Resource setting in the
Settings section so that the path to the resource file is given as an argument
to the setting. The recommended extension for resource files is .resource.
For backwards compatibility reasons also .robot, .txt and
.tsv work, but using .resource may be mandated in the future.
If the resource file path is absolute, it is used directly. Otherwise,
the resource file is first searched relatively to the directory
where the importing file is located. If the file is not found there,
it is then searched from the directories in Python's module search path.
Searching resource files from the module search path makes it possible to
bundle them into Python packages as package data and importing
them like package/example.resource.
The resource file path can contain variables, and it is recommended to use
them to make paths system-independent (for example,
${RESOURCES}/login.resource or just ${RESOURCE_PATH}).
Additionally, forward slashes (/) in the path
are automatically changed to backslashes (\) on Windows.
The user keywords and variables defined in a resource file are available in the file that takes that resource file into use. Similarly available are also all keywords and variables from the libraries, resource files and variable files imported by the said resource file.
Note
The .resource extension is new in Robot Framework 3.1.
Resource file structure¶
The higher-level structure of resource files is the same as that of suite files otherwise, but they cannot contain tests or tasks. Additionally, the Setting section in resource files can contain only imports (Library, Resource, Variables), Documentation and Keyword Tags. The Variable section and Keyword section are used exactly the same way as in suite files.
If several resource files have a user keyword with the same name, they must be used so that the keyword name is prefixed with the resource file name without the extension (for example, myresources.Some Keyword and common.Some Keyword). Moreover, if several resource files contain the same variable, the one that is imported first is taken into use.
Documenting resource files¶
Keywords created in a resource file can be documented using [Documentation] setting. The resource file itself can have Documentation in the Setting section similarly as suites.
Libdoc and various editors use these documentations, and they are naturally available for anyone opening resource files. The first logical line of the documentation of a keyword, until the first empty line, is logged when the keyword is run, but otherwise resource file documentation is ignored during the test execution.
Example resource file¶
Resource files using reStructuredText format¶
The reStructuredText data format that can be used with suite files works
also with resource files. Such resource files can use either .rst
or .rest extension and they are otherwise imported exactly as
normal resource files:
When parsing resource files using the reStructuredText format, Robot Framework
ignores all data outside code blocks containing Robot Framework data exactly
the same way as when parsing reStructuredText suite files.
For example, the following resource file imports OperatingSystem library,
defines ${MESSAGE} variable and creates My Keyword keyword:
Resource files using Markdown format¶
The Markdown data format that can be used with suite files works also with
resource files. Such resource files can use either .md or
.markdown extension and they are otherwise imported exactly as normal
resource files:
When parsing resource files using the Markdown format, Robot Framework
ignores all data outside fenced code blocks with the robotframework or robot
language tag exactly the same way as when parsing Markdown suite files.
For example, the following resource file imports OperatingSystem library,
defines ${MESSAGE} variable and creates My Keyword keyword:
Resource files using JSON format¶
Resource files can be created using JSON the same way as suite files.
Such JSON resource files must use either the standard .json extension
or the custom .rsrc extension. They are otherwise imported exactly as
normal resource files:
Resource files can be converted to JSON using ResourceFile.to_json and recreated using ResourceFile.from_json: