Skip to content

Commit b26a664

Browse files
committed
UG: Documented new --pre(run|rebot)modifier options (robotframework#1976).
Also enhanced documentation related to imporing libraries that was referenced by the new docs.
1 parent c3e8500 commit b26a664

File tree

9 files changed

+131
-21
lines changed

9 files changed

+131
-21
lines changed

doc/api/code_examples/select_every_xth_test.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33

44
class SelectEveryXthTest(SuiteVisitor):
5+
"""Visitor that keeps only every Xth test in the visited suite structure."""
56

67
def __init__(self, x, start=0):
78
self.x = int(x)

doc/userguide/src/Appendices/AvailableSettings.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ importing libraries, resources, and variables.
2020
+-----------------+--------------------------------------------------------+
2121
| Name | Description |
2222
+=================+========================================================+
23-
| Library | Used for `taking test libraries into use`_. |
23+
| Library | Used for `importing libraries`_. |
2424
+-----------------+--------------------------------------------------------+
2525
| Resource | Used for `taking resource files into use`_. |
2626
+-----------------+--------------------------------------------------------+

doc/userguide/src/Appendices/CommandLineOptions.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ Command line options for test execution
6868
--exitonerror `Stops test execution <Stopping on parsing or execution error_>`__
6969
if any error occurs when parsing test data, importing libraries, and so on.
7070
--skipteardownonexit `Skips teardowns`_ is test execution is prematurely stopped.
71+
--prerunmodifier <name:args> Activate `programmatic modification of test data`_.
72+
--prerebotmodifier <name:args> Activate `programmatic modification of results`_.
7173
--randomize <all|suites|tests|none> `Randomizes`_ test execution order.
7274
-W, --monitorwidth <chars> `Sets the width`_ of the console output.
7375
-C, --monitorcolors <on|off|force> `Specifies are colors`_ used on the console.
@@ -127,6 +129,8 @@ Command line options for post-processing outputs
127129
in test cases. Error codes are returned normally.
128130
--processemptysuite Processes output files even if files contain
129131
`empty test suites`_.
132+
--prerebotmodifier <name:args> Activate `programmatic modification of results`_.
133+
-P, --pythonpath <path> Additional locations to add to the `module search path`_.
130134
-E, --escape <what:with> `Escapes characters`_ that are problematic in the console.
131135
-A, --argumentfile <path> A text file to `read more arguments`_ from.
132136
-h, --help Prints `usage instructions`_.

doc/userguide/src/CreatingTestData/CreatingTestCases.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,7 @@ Where named arguments are supported
338338
'''''''''''''''''''''''''''''''''''
339339

340340
As already explained, the named argument syntax works with keywords. In
341-
addition to that, it also works when `taking test libraries into use`_.
341+
addition to that, it also works when `importing libraries`_.
342342

343343
Naming arguments is supported by `user keywords`_ and by most `test libraries`_.
344344
The only exception are Java based libraries that use the `static library API`_.

doc/userguide/src/CreatingTestData/UsingTestLibraries.rst

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@ section.
1313
:depth: 2
1414
:local:
1515

16-
Taking test libraries into use
17-
------------------------------
16+
Importing libraries
17+
-------------------
1818

19-
Instructions for taking test libraries into use are given in the
20-
subsections below.
19+
Test libraries are typically imported using the :setting:`Library` setting,
20+
but it is also possible to use the :name:`Import Library` keyword.
2121

22-
Using Library setting
23-
~~~~~~~~~~~~~~~~~~~~~
22+
Using `Library` setting
23+
~~~~~~~~~~~~~~~~~~~~~~~
2424

2525
Test libraries are normally imported using the :setting:`Library`
2626
setting in the Setting table and having the library name in the
@@ -56,8 +56,8 @@ cases, all the keywords in the imported library are available in that
5656
file. With resource files, those keywords are also available in other
5757
files using them.
5858

59-
Using Import Library keyword
60-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
59+
Using `Import Library` keyword
60+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
6161

6262
Another possibility to take a test library into use is using the
6363
keyword :name:`Import Library` from the BuiltIn_ library. This keyword
@@ -79,8 +79,16 @@ make it available.
7979
\ KW From MyLibrary \ \ \
8080
=========== ================= ========== ========== ==========
8181

82-
Library search path
83-
~~~~~~~~~~~~~~~~~~~
82+
Specifying library to import
83+
----------------------------
84+
85+
Libraries to import can be specified either by using the library name
86+
or the path to the library. These approaches work the same way regardless
87+
is the library imported using the :setting:`Library` setting or the
88+
:name:`Import Library` keyword.
89+
90+
Using library name
91+
~~~~~~~~~~~~~~~~~~
8492

8593
The most common way to specify a test library to import is using its
8694
name, like it has been done in all the examples in this section. In

doc/userguide/src/ExecutingTestCases/ConfiguringExecution.rst

Lines changed: 57 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ extensions are found is a requirement for successful test execution. If
281281
you need to customize it using approaches explained below, it is often
282282
a good idea to create a custom `start-up script`_.
283283

284-
__ `Taking test libraries into use`_
284+
__ `Specifying library to import`_
285285
__ `Setting listeners`_
286286

287287
Locations automatically in module search path
@@ -444,6 +444,57 @@ Examples::
444444

445445
__ `Free test suite metadata`_
446446

447+
Programmatic modification of test data
448+
--------------------------------------
449+
450+
If the provided built-in features to modify test data before execution
451+
are not enough, Robot Framework 2.9 and newer provide a possible to do
452+
custom modifications programmatically. This is accomplished by creating
453+
a model modifier and activating it using the :option:`--prerunmodifier`
454+
option.
455+
456+
Model modifiers should be implemented as visitors that can traverse through
457+
the executable test suite structure and modify it as needed. The visitor
458+
interface is explained as part of the `Robot Framework API documentation`__,
459+
and the example below ought to give an idea of how it can be used and how
460+
powerful this functionality is.
461+
462+
.. sourcecode:: python
463+
464+
../api/code_examples/select_every_xth_test.py
465+
466+
When a model modifier is taken into use on the command line using the
467+
:option:`--prerunmodifier` option, it can be specified either as a name of
468+
the modifier class or a path to the modifier file. If the modifier is given
469+
as a class name, the module containing the class must be in the `module search
470+
path`_, and if the module name is different than the class name, the given
471+
name must include both like `module.ModifierClass`. If the modifier is given
472+
as a path, the class name must be same as the file name. For most parts this
473+
works exactly like when `specifying a test library to import`__.
474+
475+
If a modifier requires arguments, like the example above does, they can be
476+
specified after the modifier name or path using either a colon (`:`) or a
477+
semicolon (`;`) as a separator. If both are used in the value, the one first
478+
is considered the actual separator.
479+
480+
For example, if the above model modifier would be in a file
481+
:file:`SelectEveryXthTest.py`, it could be used like this::
482+
483+
# Specify the modifier as a path. Run every second test.
484+
pybot --prerunmodifier path/to/SelectEveryXthTest.py:2 tests.robot
485+
486+
# Specify the modifier as a name. Run every third test, starting from the second.
487+
# SelectEveryXthTest.py must be in the module search path.
488+
pybot --prerunmodifier SelectEveryXthTest:3:1 tests.robot
489+
490+
If more than one model modifier is needed, they can be specified by using
491+
the :option:`--prerunmodifier` option multiple times. If similar modifying
492+
is needed before creating results, `programmatic modification of results`_
493+
can be enabled using the :option:`--prerebotmodifier` option.
494+
495+
__ https://robot-framework.readthedocs.org/en/latest/autodoc/robot.model.html#module-robot.model.visitor
496+
__ `Specifying library to import`_
497+
447498
Controlling console output
448499
--------------------------
449500

@@ -509,7 +560,8 @@ case-insensitive values:
509560
Setting listeners
510561
-----------------
511562

512-
So-called listeners_ can be used for monitoring the test
513-
execution. They are taken into use with the command line option
514-
:option:`--listener`, and the specified listeners must be in the `module
515-
search path`_ similarly as test libraries.
563+
Listeners_ can be used to monitor the test execution. When they are taken into
564+
use from the command line, they are specified using the :option:`--listener`
565+
command line option. The value can either be a path to a listener or
566+
a listener name. See the `Using listener interface`_ section for more details
567+
about importing listeners and using them in general.

doc/userguide/src/ExecutingTestCases/OutputFiles.rst

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -575,6 +575,51 @@ Examples::
575575
rebot --starttime 20080611-175920 --endtime 20080611-180242 *.xml
576576
rebot --starttime 20110302-1317 --endtime 20110302-11418 myoutput.xml
577577

578+
Programmatic modification of results
579+
------------------------------------
580+
581+
If the provided built-in features to modify results are are not enough,
582+
Robot Framework 2.9 and newer provide a possible to do custom modifications
583+
programmatically. This is accomplished by creating a model modifier and
584+
activating it using the :option:`--prerebotmodifier` option.
585+
586+
This functionality works nearly exactly like `programmatic modification of
587+
test data`_ that can be enabled with the :option:`--prerunmodifier` option.
588+
The only difference is that the modified model is Robot Framework's
589+
result model and not the executable test suite model. For example, the
590+
following modifier marks all passed tests that have taken more time than
591+
allowed as failed:
592+
593+
.. sourcecode:: python
594+
595+
from robot.api import SuiteVisitor
596+
597+
598+
class ExecutionTimeChecker(SuiteVisitor):
599+
600+
def __init__(self, max_seconds):
601+
self.max_milliseconds = float(max_seconds) * 1000
602+
603+
def visit_test(self, test):
604+
if test.status == 'PASS' and test.elapsedtime > self.max_milliseconds:
605+
test.status = 'FAIL'
606+
test.message = 'Test execution took too long.'
607+
608+
If the above modifier would be in file :file:`ExecutionTimeChecker.py`, it
609+
could be used, for example, like this::
610+
611+
# Specify modifier as a path when running tests. Maximum time is 42 seconds.
612+
pybot --prerebotmodifier path/to/ExecutionTimeChecker.py:42 tests.robot
613+
614+
# Specify modifier as a name when using Rebot. Maximum time is 3.14 seconds.
615+
# ExecutionTimeChecker.py must be in the module search path.
616+
rebot --prerebotmodifier ExecutionTimeChecker:3.14 output.xml
617+
618+
If more than one model modifier is needed, they can be specified by using
619+
the :option:`--prerebotmodifier` option multiple times. When executing tests,
620+
it is possible to use :option:`--prerunmodifier` and
621+
:option:`--prerebotmodifier` options together.
622+
578623
System log
579624
----------
580625

doc/userguide/src/ExtendingRobotFramework/CreatingTestLibraries.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1705,7 +1705,7 @@ Since libraries are normal programming code, they can be packaged
17051705
using normal packaging tools. With Python, good options include
17061706
distutils_, contained by Python's standard library, and the newer
17071707
setuptools_. A benefit of these tools is that library modules are
1708-
installed into a location that is automatically in the `library
1708+
installed into a location that is automatically in the `module
17091709
search path`_.
17101710

17111711
When using Java, it is natural to package libraries into a JAR

src/robot/model/visitor.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,9 @@
5252
Examples
5353
--------
5454
55-
The following example modifies the test suite structure so that it keeps only
56-
every Xth test. It could be used, for example, with Robot Framework's
57-
``--prerunmodifier`` option to modify test data before execution.
55+
The following example visitor modifies the test suite structure it visits.
56+
It could be used, for example, with Robot Framework's ``--prerunmodifier``
57+
option to modify test data before execution.
5858
5959
.. literalinclude:: /../../doc/api/code_examples/select_every_xth_test.py
6060

0 commit comments

Comments
 (0)