Skip to content

Commit 2ca377f

Browse files
committed
UG: Document JSON data format (#3902) and enhance related docs.
Includes: - Using JSON suite files. - Using JSON resource files. - Using reST resource files. - Recommend `.resource` with normal resource files more strongly. - List all supported extensions under Registrations. Also document reST resource files.
1 parent 4f9e8d2 commit 2ca377f

File tree

3 files changed

+259
-24
lines changed

3 files changed

+259
-24
lines changed

doc/userguide/src/Appendices/Registrations.rst

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,44 @@ Registrations
44
This appendix lists file extensions, media types, and so on, that are
55
associated with Robot Framework.
66

7-
File extensions
8-
---------------
7+
Suite file extensions
8+
---------------------
99

10-
- Robot Framework `suite files`_ use the :file:`.robot` extension.
11-
- Robot Framework `resource files`_ use the :file:`.resource` extension.
10+
`Suite files`_ with the following extensions are parsed automatically:
11+
12+
:file:`.robot`
13+
Suite file using the `plain text format`_.
14+
15+
:file:`.robot.rst`
16+
Suite file using the `reStructuredText format`_.
17+
18+
:file:`.rbt`
19+
Suite file using the `JSON format`_.
20+
21+
Using other extensions is possible, but it requires `separate configuration`__.
22+
23+
__ `Selecting files to parse`_
24+
25+
Resource file extensions
26+
------------------------
27+
28+
`Resource files`_ can use the following extensions:
29+
30+
:file:`.resource`
31+
Recommended when using the plain text format.
32+
33+
:file:`.robot`, :file:`.txt` and :file:`.tsv`
34+
Supported with the plain text format for backwards compatibility reasons.
35+
:file:`.resource` is recommended and may be mandated in the future.
36+
37+
:file:`.rst` and :file:`.rest`
38+
Resource file using the `reStructuredText format`__.
39+
40+
:file:`.rsrc` and :file:`.json`
41+
Resource file using the `JSON format`__.
42+
43+
__ `Resource files using reStructured text format`_
44+
__ `Resource files using JSON format`_
1245

1346
Media type
1447
----------

doc/userguide/src/CreatingTestData/ResourceAndVariableFiles.rst

Lines changed: 98 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ User keywords and variables in `suite files`_ and `suite
55
initialization files`_ can only be used in files where they are
66
created, but *resource files* provide a mechanism for sharing them.
77
The high level syntax for creating resource files is exactly the same
8-
as when creating test case files and `supported file formats`_ are the same
8+
as when creating suite files and `supported file formats`_ are the same
99
as well. The main difference is that resource files cannot have tests.
1010

1111
*Variable files* provide a powerful mechanism for creating and sharing
@@ -21,13 +21,20 @@ also makes them somewhat more complicated than `Variable sections`_.
2121
Resource files
2222
--------------
2323

24+
Resource files are typically created using the plain text format, but also
25+
`reStructuredText format`__ and `JSON format`__ are supported.
26+
27+
__ `Resource files using reStructured text format`_
28+
__ `Resource files using JSON format`_
29+
2430
Taking resource files into use
2531
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2632

2733
Resource files are imported using the :setting:`Resource` setting in the
2834
Settings section so that the path to the resource file is given as an argument
29-
to the setting. The recommended extension for resource files is
30-
:file:`.resource`, but also the normal :file:`.robot` extension works.
35+
to the setting. The recommended extension for resource files is :file:`.resource`.
36+
For backwards compatibility reasons also :file:`.robot`, :file:`.txt` and
37+
:file:`.tsv` work, but using :file:`.resource` may be mandated in the future.
3138

3239
If the resource file path is absolute, it is used directly. Otherwise,
3340
the resource file is first searched relatively to the directory
@@ -47,7 +54,7 @@ are automatically changed to backslashes (:codesc:`\\`) on Windows.
4754

4855
*** Settings ***
4956
Resource example.resource
50-
Resource ../data/resources.robot
57+
Resource ../resources/login.resource
5158
Resource package/example.resource
5259
Resource ${RESOURCES}/common.resource
5360

@@ -65,11 +72,12 @@ Resource file structure
6572
~~~~~~~~~~~~~~~~~~~~~~~
6673

6774
The higher-level structure of resource files is the same as that of
68-
test case files otherwise, but, of course, they cannot contain Test
69-
Case sections. Additionally, the Setting section in resource files can
70-
contain only import settings (:setting:`Library`, :setting:`Resource`,
71-
:setting:`Variables`) and :setting:`Documentation`. The Variable section and
72-
Keyword section are used exactly the same way as in test case files.
75+
suite files otherwise, but they cannot contain tests or tasks.
76+
Additionally, the Setting section in resource files can contain only imports
77+
(:setting:`Library`, :setting:`Resource`, :setting:`Variables`),
78+
:setting:`Documentation` and :setting:`Keyword Tags`.
79+
The Variable section and Keyword section are used exactly the same way
80+
as in suite files.
7381

7482
If several resource files have a user keyword with the same name, they
7583
must be used so that the `keyword name is prefixed with the resource
@@ -126,6 +134,87 @@ Example resource file
126134
[Arguments] ${password}
127135
Input Text password_field ${password}
128136

137+
Resource files using reStructured text format
138+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
139+
140+
The `reStructuredText format`_ that can be used with `suite files`_ works
141+
also with resource files. Such resource files can use either :file:`.rst`
142+
or :file:`.rest` extension and they are otherwise imported exactly as
143+
normal resource files:
144+
145+
.. sourcecode:: robotframework
146+
147+
*** Settings ***
148+
Resource example.rst
149+
150+
When parsing resource files using the reStructuredText format, Robot Framework
151+
ignores all data outside code blocks containing Robot Framework data exactly
152+
the same way as when parsing `reStructuredText suite files`__.
153+
For example, the following resource file imports :name:`OperatingSystem` library,
154+
defines `${MESSAGE}` variable and creates :name:`My Keyword` keyword:
155+
156+
.. sourcecode:: rest
157+
158+
Resource file using reStructuredText
159+
------------------------------------
160+
161+
This text is outside code blocks and thus ignored.
162+
163+
.. code:: robotframework
164+
165+
*** Settings ***
166+
Library OperatingSystem
167+
168+
*** Variables ***
169+
${MESSAGE} Hello, world!
170+
171+
Also this text is outside code blocks and ignored. Code blocks not
172+
containing Robot Framework data are ignored as well.
173+
174+
.. code:: robotframework
175+
176+
# Both space and pipe separated formats are supported.
177+
178+
| *** Keywords *** | | |
179+
| My Keyword | [Arguments] | ${path} |
180+
| | Directory Should Exist | ${path} |
181+
182+
__ `reStructuredText format`_
183+
184+
Resource files using JSON format
185+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
186+
187+
Resource files can be created using JSON_ the `same way as suite files`__.
188+
Such JSON resource files must use either the standard :file:`.json` extension
189+
or the custom :file:`.rsrc` extension. They are otherwise imported exactly as
190+
normal resource files:
191+
192+
.. sourcecode:: robotframework
193+
194+
*** Settings ***
195+
Resource example.rsrc
196+
197+
Resource files can be converted to JSON using `ResourceFile.to_json`__ and
198+
recreated using `ResourceFile.from_json`__:
199+
200+
.. sourcecode:: python
201+
202+
from robot.running import ResourceFile
203+
204+
205+
# Create resource file based on data on the file system.
206+
resource = ResourceFile.from_file_system('example.resource')
207+
208+
# Save JSON data to a file.
209+
resource.to_json('example.rsrc')
210+
211+
# Recreate resource from JSON data.
212+
resource = ResourceFile.from_json('example.rsrc')
213+
214+
__ `JSON format`_
215+
__ https://robot-framework.readthedocs.io/en/master/autodoc/robot.running.html#robot.running.model.ResourceFile.to_json
216+
__ https://robot-framework.readthedocs.io/en/master/autodoc/robot.running.html#robot.running.model.ResourceFile.from_json
217+
129218
Variable files
130219
--------------
131220

doc/userguide/src/CreatingTestData/TestDataSyntax.rst

Lines changed: 124 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -106,17 +106,21 @@ and their arguments, are separated from each others with two or more spaces.
106106
An alternative is using the `pipe separated format`_ where the separator is
107107
the pipe character surrounded with spaces (:codesc:`\ |\ `).
108108

109-
Executed files typically use the :file:`.robot` extension, but that `can be
110-
configured`__ with the :option:`--extension` option. `Resource files`_
111-
can use the :file:`.robot` extension as well, but using the dedicated
112-
:file:`.resource` extension is recommended. Files containing non-ASCII
109+
Suite files typically use the :file:`.robot` extension, but what files are
110+
parsed `can be configured`__. `Resource files`_ can use the :file:`.robot`
111+
extension as well, but using the dedicated :file:`.resource` extension is
112+
recommended and may be mandated in the future. Files containing non-ASCII
113113
characters must be saved using the UTF-8 encoding.
114114

115-
Robot Framework also supports reStructuredText_ files so that normal
116-
Robot Framework data is `embedded into code blocks`__. It is possible to
117-
use either :file:`.rst` or :file:`.rest` extension with reStructuredText
118-
files, but the aforementioned :option:`--extension` option `must be used`__
119-
to enable parsing them when executing a directory.
115+
Robot Framework supports also reStructuredText_ files so that normal
116+
Robot Framework data is `embedded into code blocks`__. Only files with
117+
the :file:`.robot.rst` extension are parsed by default. If you would
118+
rather use just :file:`.rst` or :file:`.rest` extension, that needs to be
119+
configured separately.
120+
121+
Robot Framework data can also be created in `JSON format`_ that is targeted
122+
more for tool developers than normal Robot Framework users. Only JSON files
123+
with the custom :file:`.rbt` extension are parsed by default.
120124

121125
Earlier Robot Framework versions supported data also in HTML and TSV formats.
122126
The TSV format still works if the data is compatible with the `space separated
@@ -129,9 +133,9 @@ format at all.
129133

130134
__ `Selecting files to parse`_
131135
__ `reStructuredText format`_
132-
__ `Selecting files to parse`_
133136

134137
.. _space separated plain text format:
138+
.. _plain text format:
135139

136140
Space separated format
137141
~~~~~~~~~~~~~~~~~~~~~~
@@ -314,7 +318,116 @@ when processing files using reStructuredText tooling normally.
314318
JSON format
315319
~~~~~~~~~~~
316320

317-
FIXME
321+
Robot Framework supports data also in JSON_ format. This format is designed
322+
more for tool developers than for regular Robot Framework users and it is not
323+
meant to be edited manually. Its most important use cases are:
324+
325+
- Transferring data between processes and machines. A suite can be converted
326+
to JSON in one machine and recreated somewhere else.
327+
- Saving a suite constructed from normal Robot Framework data into a single
328+
JSON file that is faster to parse.
329+
- Alternative data format for external tools generating tests or tasks.
330+
331+
.. note:: The JSON data support is new in Robot Framework 6.1 and it can be
332+
enhanced in future Robot Framework versions. If you have an enhancement
333+
idea or believe you have encountered a bug, please submit an issue__
334+
or start a discussion thread on the `#devel` channel on our Slack_.
335+
336+
__ https://issues.robotframework.org
337+
338+
Converting suite to JSON
339+
''''''''''''''''''''''''
340+
341+
A suite structure can be serialized into JSON by using the `TestSuite.to_json`__
342+
method. When used without arguments, it returns JSON data as a string, but
343+
it also accepts a path or an open file where to write JSON data along with
344+
configuration options related to JSON formatting:
345+
346+
.. sourcecode:: python
347+
348+
from robot.running import TestSuite
349+
350+
351+
# Create suite based on data on the file system.
352+
suite = TestSuite.from_file_system('/path/to/data')
353+
# Get JSON data as a string.
354+
data = suite.to_json()
355+
# Save JSON data to a file with custom indentation.
356+
suite.to_json('data.rbt', indent=2)
357+
358+
If you would rather work with Python data and then convert that to JSON
359+
or some other format yourself, you can use `TestSuite.to_dict`__ instead.
360+
361+
__ https://robot-framework.readthedocs.io/en/master/autodoc/robot.running.html#robot.running.model.TestSuite.to_json
362+
__ https://robot-framework.readthedocs.io/en/master/autodoc/robot.running.html#robot.running.model.TestSuite.to_dict
363+
364+
Creating suite from JSON
365+
''''''''''''''''''''''''
366+
367+
A suite can be constructed from JSON data using the `TestSuite.from_json`__
368+
method. It works both with JSON strings and paths to JSON files:
369+
370+
.. sourcecode:: python
371+
372+
from robot.running import TestSuite
373+
374+
375+
# Create suite from JSON data in a file.
376+
suite = TestSuite.from_json('data.rbt')
377+
# Create suite from a JSON string.
378+
suite = TestSuite.from_json('{"name": "Suite", "tests": [{"name": "Test"}]}')
379+
380+
If you have data as a Python dictionary, you can use `TestSuite.from_dict`__
381+
instead.
382+
383+
__ https://robot-framework.readthedocs.io/en/master/autodoc/robot.running.html#robot.running.model.TestSuite.from_json
384+
__ https://robot-framework.readthedocs.io/en/master/autodoc/robot.running.html#robot.running.model.TestSuite.from_dict
385+
386+
Executing JSON files
387+
''''''''''''''''''''
388+
389+
When using the `robot` command normally, JSON files with the :file:`.rbt`
390+
extension are parsed automatically. This includes running individual JSON files
391+
like `robot tests.rbt` and running directories containing :file:`.rbt` files.
392+
If you would rather use the standard :file:`.json` extension, you need to
393+
`configure which files are parsed`__.
394+
395+
__ `Selecting files to parse`_
396+
397+
Adjusting suite source
398+
''''''''''''''''''''''
399+
400+
Suite source in the data got from `TestSuite.to_json` and `TestSuite.to_dict`
401+
is in absolute format. If a suite is recreated later on a different machine,
402+
the source may thus not match the directory structure on that machine. To
403+
avoid that, it is possible to use the `TestSuite.adjust_source`__ method to
404+
make the suite source relative before getting the data and add a correct root
405+
directory after the suite is recreated:
406+
407+
.. sourcecode:: python
408+
409+
from robot.running import TestSuite
410+
411+
412+
# Create a suite, adjust source and convert to JSON.
413+
suite = TestSuite.from_file_system('/path/to/data')
414+
suite.adjust_source(relative_to='/path/to')
415+
suite.to_json('data.rbt')
416+
417+
# Recreate suite elsewhere and adjust source accordingly.
418+
suite = TestSuite.from_json('data.rbt')
419+
suite.adjust_source(root='/new/path/to')
420+
421+
__ https://robot-framework.readthedocs.io/en/master/autodoc/robot.running.html#robot.running.model.TestSuite.adjust_source
422+
423+
JSON structure
424+
''''''''''''''
425+
426+
Imports, variables and keywords created in suite files are included in the
427+
generated JSON along with tests and tasks. The exact JSON structure is documented
428+
in the :file:`running.json` `schema file`__.
429+
430+
__ https://github.com/robotframework/robotframework/tree/master/doc/schema#readme
318431

319432
Rules for parsing the data
320433
--------------------------

0 commit comments

Comments
 (0)