You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Support serializing executable suite into JSON #3902
It would be convenient to be able to serialize executable TestSuite objects into JSON that can be then also be used to recreate same suite later. Most important use cases are:
Possibility to save a suite, possible a nested suite, constructed from data on file system into a single file that is faster to parse.
Alternative data format for external tools generating tests.
In the subsequent comments I'll go through some design ideas I have related to this. Please use 👍 and 👎 to indicate do you like those ideas as well as this issue in general. Written comments obviously appreciated as well!
Reacted by Maciej Wiczk, Rob McCarthy, Rolando Sanches, Apolayo27, Norbert Wietschorke, yer, JLBIZ, Jonas Lauer and Jian Zhou
I propose we add to_json method to TestSuite. It should by default return JSON string but it should also accept a path or an open file object where to save the JSON. It could also accept an option to specify should the object be serialized into a single JSON object or split to multiple. I write a separate comment about that.
We should also add from_json classmethod to TestSuite for constructing a suite based on JSON data.
We probably should add to_json and from_json also to TestCase, Keyword, If and For objects.
Serializing a big suite into JSON is problematic because reading it back requires deserializing the big JSON string first. To avoid problems with that, I propose we support splitting suites into multiple objects using this algorithm:
The top level suite is one object.
Suites, tests, setup and teardown a suite has are separate objects. Each separate object has a growing index. The parent suite has this index in the place of the real object.
All objects are stored into a single file, one object per line. This is not valid JSON but streaming JSON like this is pretty common. The format is also standardized but annoyingly there are two seemingly identical standards JSON Lines and Newline Delimited JSON. (The latter seems to be gone. See update 3 below.)
Child object index is got directly from the order they are serialized. The first child has index 1, the second index 2, and so on.
I propose we support both "one big object" and "split object" formats when construction model objects using from_json. The to_json should get an optional split argument. I think it should be False by default to produce valid JSON unless explicitly configured.
Body items need a type to separate normal keywords from setups and teardowns as well as from IFs and FORs. We probably could omit the type with normal keywords (the common case) to make the file size smaller.
Above examples use spaces between items but I believe we should omit them to reduce file size.
UPDATE: Setups and teardowns are nowadays stored in model objects outside the body. There thus shouldn't be need save their type separately. Something like this ought to be clear enough:
We probably needed to separate keywords from control structures such as IF, though, so possibly having types would be a good idea anyway.
UPDATE 2: Splitting JSON like this won't be implemented as part of RF 6.1 where the JSON serialization will be introduced. We can return to this later if/when there are need.
UPDATE 3: It seems that Newline Delimited JSON is gone (http://ndjson.org contains only gambling ads) so there are no competing streaming JSON standards anymore. If we look at streaming again, we can simply use JSON Lines.
I propose we use new .rbt extension when saving this format into a file. We cannot really use .json if we support also JSON Lines and having a custom extension also separates these files from other JSON files. We cannot use .robot because the format is different but we can consider .rbt to mean "condensed robot".
We should allow running robot suite.rbt as well as robot directory where the directory contains these files.
We should have a way to convert a file or a directory into a .rbt file. One easy way would be adding a command line option to robot that could be used like robot --serialize tests.rbt tests.robot. Being able to configure saved data like robot --name Smoke --include smoke --serialize smoke.rbt tests would be handy, but possible additional options controlling serialization would then be mixed with "normal" options. Alternatively we could add a separate entry point that could be called like python -m robot.serialize tests.rbt and enhancing the long neglected Testdoc tool (python -m robot.testdoc) to support this would be possible as well.
.rbt files saved using this mechanism should use the "split object" approach.
UPDATE: RF 6.1 will support .rbt files but there most likely won't be separate entry point for creating them. They can be created programmatically, though, and a separate entry point can be added later.
External resource files could possibly be saved into a .rbt file as well. The best approach would be including all imported resources in it automatically but this probably should be configurable. The biggest problem would be designing how to change imports so that original example.resource would point to an embedded resource. This requires changes to the import machinery and probably needs a separate issue. Could also be implemented later.
This issue is highly related to the proposal to add JSON based output format (#3423). Executable suite structure and result suite structure are similar, they even have common base, and they should use the same approach to serialize them to JSON. I think splitting a suite into multiple objects like I propose above ought to work fine also with result suites. They couldn't use the proposed .rbt extension, though, because they contain different data and we don't want robot directory to try running possible result files.
The biggest reason to split JSON into multiple objects is to avoid the need to read the whole file into memory and parse the whole JSON in one go. This may not be a huge problem with data, but if this same approach is used with results (#3423) it's very important because I've seen output.xml files that are over 1GB.
Open the file and iterate over it like for line in file to read only one line at a time.
Process the first line. Construct TestSuite object with specified name and doc. Tests are not specified directly (there are integers, not objects) so add callbacks to add them when child objects with id 1 and 2 are encountered.
Process the second line. This line has implicit id 1 and thus a callback created earlier is called with this data creating a TestCase object that's added to the earlier suite. Tests body has one item referred with id 3. This creates another callback.
Process the second line (id 2). Call relevant callback to created another TestCases.
Process the last line (id 3). Call relevant callback to created Keyword that's added to the first test.
We could extend this mechanism to strings so remove duplication. For example, the first line of the above example could be changed to this:
This way if we ever need to use string "Suite" again, we just use 1 in place of it. This particular string may not be used that often and the string is short, but, for example, keyword documentation strings can be long and very often repeated. Possibly we should use this approach only with long strings and with strings we know are used very often like "PASS" and "KEYWORD".
We use similar approach, created years ago by @mkorpela and @jussimalinen, to remove duplication in log.html and report.html and there it has worked great. There we also zip long strings but I'm not sure is that needed here. It's easier and probably better to zip the whole file when needed instead. There we also represent all timestamps as integers representing milliseconds as a difference to a base time set only once. That means that instead of using e.g. 20210326 19:55:01.123 and 20210326 19:55:01.124, we'd just have base time 20210326 19:55:01.123 once and timestamps would be just 0 and 1. This isn't relevant at all with data but something to consider with results.
The above commit added support for JSON resource files and enhanced error handling if a parsed JSON file is invalid. With resource files it is possible to use either *.json or *.rsrc extension.
The source path is stored in absolute format. This causes problems if data is moved to another machine with a different directory layout. The main problem is that imports that use relative paths are relative to the source. This is a pretty bad limitation, but I'm not sure what's the best way to handle it. Possibly to_dict/json could allow making the source relative to a certain directory, and from_dict/json could then turn the relative path to an absolute path based on where the source file is located. This could possibly still be fixed in RF 6.1 but I don't consider it mandatory.
Resource and variable files imported by a suite cannot be bundled with it. Fixing this would require making it possible to resolve imports without executing tests/tasks. Too much work for RF 6.1.
In the previous comment I mentioned that absolute suite source is problematic. The commit above added TestSuite.adjust_source that can be used to make absolute source relative and to add a new root to a relative source. That's especially convenient when moving JSON data around. This example is from the docstring of the new method:
fromrobot.runningimportTestSuite# Create a suite, adjust source and convert to JSON. suite=TestSuite.from_file_system('/path/to/data')
suite.adjust_source(relative_to='/path/to')
suite.to_json('data.json')
# Recreate suite elsewhere and adjust source accordingly.suite=TestSuite.from_json('data.json')
suite.adjust_source(root='/new/path/to')
After this enhancement, I consider JSON serialization support to be good enough for RF 6.1. Documentation is still missing, but I'll write it after the first release candidate is out.
Commits listed above have added a JSON schema as well as documentation in the User Guide. Creating JSON resource files was made more convenient by #4793. We can finally consider this issue done.
Activity
pekkaklarck commentedon Mar 23, 2021
I propose we add
to_jsonmethod toTestSuite. It should by default return JSON string but it should also accept a path or an open file object where to save the JSON. It could also accept an option to specify should the object be serialized into a single JSON object or split to multiple. I write a separate comment about that.We should also add
from_jsonclassmethod toTestSuitefor constructing a suite based on JSON data.We probably should add
to_jsonandfrom_jsonalso toTestCase,Keyword,IfandForobjects.pekkaklarck commentedon Mar 23, 2021
Serializing a big suite into JSON is problematic because reading it back requires deserializing the big JSON string first. To avoid problems with that, I propose we support splitting suites into multiple objects using this algorithm:
I propose we support both "one big object" and "split object" formats when construction model objects using
from_json. Theto_jsonshould get an optionalsplitargument. I think it should beFalseby default to produce valid JSON unless explicitly configured.An example of a suite as one object:
Same suite split to multiple objects:
Body items need a type to separate normal keywords from setups and teardowns as well as from IFs and FORs. We probably could omit the type with normal keywords (the common case) to make the file size smaller.
Above examples use spaces between items but I believe we should omit them to reduce file size.
UPDATE: Setups and teardowns are nowadays stored in model objects outside the
body. There thus shouldn't be need save their type separately. Something like this ought to be clear enough:We probably needed to separate keywords from control structures such as
IF, though, so possibly having types would be a good idea anyway.UPDATE 2: Splitting JSON like this won't be implemented as part of RF 6.1 where the JSON serialization will be introduced. We can return to this later if/when there are need.
UPDATE 3: It seems that Newline Delimited JSON is gone (
http://ndjson.orgcontains only gambling ads) so there are no competing streaming JSON standards anymore. If we look at streaming again, we can simply use JSON Lines.pekkaklarck commentedon Mar 23, 2021
I propose we use new
.rbtextension when saving this format into a file. We cannot really use.jsonif we support also JSON Lines and having a custom extension also separates these files from other JSON files. We cannot use.robotbecause the format is different but we can consider.rbtto mean "condensed robot".We should allow running
robot suite.rbtas well asrobot directorywhere the directory contains these files.We should have a way to convert a file or a directory into a
.rbtfile. One easy way would be adding a command line option torobotthat could be used likerobot --serialize tests.rbt tests.robot. Being able to configure saved data likerobot --name Smoke --include smoke --serialize smoke.rbt testswould be handy, but possible additional options controlling serialization would then be mixed with "normal" options. Alternatively we could add a separate entry point that could be called likepython -m robot.serialize tests.rbtand enhancing the long neglected Testdoc tool (python -m robot.testdoc) to support this would be possible as well..rbtfiles saved using this mechanism should use the "split object" approach.UPDATE: RF 6.1 will support
.rbtfiles but there most likely won't be separate entry point for creating them. They can be created programmatically, though, and a separate entry point can be added later.pekkaklarck commentedon Mar 23, 2021
Saved suite objects in
.rbtfiles should also contain imports, variables and keywords same way as normal.robotfiles can have.pekkaklarck commentedon Mar 23, 2021
External resource files could possibly be saved into a
.rbtfile as well. The best approach would be including all imported resources in it automatically but this probably should be configurable. The biggest problem would be designing how to change imports so that originalexample.resourcewould point to an embedded resource. This requires changes to the import machinery and probably needs a separate issue. Could also be implemented later.pekkaklarck commentedon Mar 23, 2021
This issue is highly related to the proposal to add JSON based output format (#3423). Executable suite structure and result suite structure are similar, they even have common base, and they should use the same approach to serialize them to JSON. I think splitting a suite into multiple objects like I propose above ought to work fine also with result suites. They couldn't use the proposed .rbt extension, though, because they contain different data and we don't want
robot directoryto try running possible result files.pekkaklarck commentedon Mar 26, 2021
The biggest reason to split JSON into multiple objects is to avoid the need to read the whole file into memory and parse the whole JSON in one go. This may not be a huge problem with data, but if this same approach is used with results (#3423) it's very important because I've seen output.xml files that are over 1GB.
Assuming we have this JSON from the earlier example
it could be processed like this:
for line in fileto read only one line at a time.TestSuiteobject with specifiednameanddoc. Tests are not specified directly (there are integers, not objects) so add callbacks to add them when child objects with id 1 and 2 are encountered.TestCaseobject that's added to the earlier suite. Tests body has one item referred with id 3. This creates another callback.TestCases.Keywordthat's added to the first test.We could extend this mechanism to strings so remove duplication. For example, the first line of the above example could be changed to this:
This way if we ever need to use string "Suite" again, we just use
1in place of it. This particular string may not be used that often and the string is short, but, for example, keyword documentation strings can be long and very often repeated. Possibly we should use this approach only with long strings and with strings we know are used very often like "PASS" and "KEYWORD".We use similar approach, created years ago by @mkorpela and @jussimalinen, to remove duplication in log.html and report.html and there it has worked great. There we also zip long strings but I'm not sure is that needed here. It's easier and probably better to zip the whole file when needed instead. There we also represent all timestamps as integers representing milliseconds as a difference to a base time set only once. That means that instead of using e.g.
20210326 19:55:01.123and20210326 19:55:01.124, we'd just have base time20210326 19:55:01.123once and timestamps would be just0and1. This isn't relevant at all with data but something to consider with results.pekkaklarck commentedon Jun 7, 2021
Too big task for RF 4.1. Hopefully can be included in RF 5.0.
37 remaining items
ModelObject enhancements
Enhance `robot.model.control`.
Enhancements and fixes to JSON serialization. #3902
pekkaklarck commentedon May 24, 2023
The above commit added support for JSON resource files and enhanced error handling if a parsed JSON file is invalid. With resource files it is possible to use either
*.jsonor*.rsrcextension.pekkaklarck commentedon May 24, 2023
There are currently the following limitations:
to_dict/jsoncould allow making the source relative to a certain directory, andfrom_dict/jsoncould then turn the relative path to an absolute path based on where the source file is located. This could possibly still be fixed in RF 6.1 but I don't consider it mandatory.${CURDIR}doesn't work. Fixing it would basically require fixing${CURDIR}should be normal variable #596 and that's too much work for RF 6.1.The limitations listed above above need to mentioned in the documentation.
UG: Document `--parseinclude`. #4687
Add `TestSuite.adjust_source`.
pekkaklarck commentedon May 31, 2023
In the previous comment I mentioned that absolute suite source is problematic. The commit above added
TestSuite.adjust_sourcethat can be used to make absolute source relative and to add a new root to a relative source. That's especially convenient when moving JSON data around. This example is from the docstring of the new method:After this enhancement, I consider JSON serialization support to be good enough for RF 6.1. Documentation is still missing, but I'll write it after the first release candidate is out.
Add schema for `robot.running` JSON model. Part of #3902.
UG: Document JSON data format (#3902) and enhance related docs.
pekkaklarck commentedon Jun 11, 2023
Commits listed above have added a JSON schema as well as documentation in the User Guide. Creating JSON resource files was made more convenient by #4793. We can finally consider this issue done.
Add "new in" notes to `ModelObject.to/from_dict/json` methods.