|
12 | 12 | # See the License for the specific language governing permissions and |
13 | 13 | # limitations under the License. |
14 | 14 |
|
| 15 | +"""Interface to ease traversing through a test suite structure. |
| 16 | +
|
| 17 | +Visitors make it easy to modify test suite structures or to collect information |
| 18 | +from them. They work both with the :class:`executable suite <robot.running.model.TestSuite>` |
| 19 | +and the :class:`result suite <robot.result.testsuite.TestSuite>`, but the |
| 20 | +objects passed to the visitor methods are slightly different. The main |
| 21 | +difference is that the result objects have attributes like :attr:`status` and |
| 22 | +:attr:`starttime` that do not exist in the executable objects. |
| 23 | +
|
| 24 | +This module contains :class:`SuiteVisitor` that implements the core logic to |
| 25 | +visit a test suite structure, and the :mod:`~robot.result` package contains |
| 26 | +:class:`~robot.result.visitor.ResultVisitor` that supports visiting the whole |
| 27 | +test execution result structure. Both of these visitors should be imported |
| 28 | +via the :mod:`robot.api` package when used by external code. |
| 29 | +
|
| 30 | +Visitor algorithm |
| 31 | +----------------- |
| 32 | +
|
| 33 | +All suite, test, keyword and message objects have a :meth:`visit` method that |
| 34 | +accepts a visitor instance. These methods will then call the correct visitor |
| 35 | +method :meth:`~SuiteVisitor.visit_suite`, :meth:`~SuiteVisitor.visit_test`, |
| 36 | +:meth:`~SuiteVisitor.visit_keyword` or :meth:`~SuiteVisitor.visit_message`, |
| 37 | +depending on the instance where the :meth:`visit` method exists. |
| 38 | +
|
| 39 | +The recommended and definitely easiest way to implement a visitor is extending |
| 40 | +the :class:`SuiteVisitor` base class. The default implementation of its |
| 41 | +:meth:`visit_x` methods take care of traversing child elements of the object |
| 42 | +:obj:`x` recursively. A :meth:`visit_x` method first calls a corresponding |
| 43 | +:meth:`start_x` method (e.g. :meth:`visit_suite` calls :meth:`start_suite`), |
| 44 | +then calls :meth:`visit` for all child objects of the :obj:`x` object, and |
| 45 | +finally calls the corresponding :meth:`end_x` method. The default |
| 46 | +implementations of :meth:`start_x` and :meth:`end_x` do nothing. |
| 47 | +
|
| 48 | +Custom visitors can stop visiting at a certain level either by overriding |
| 49 | +suitable :meth:`visit_x` method or by returning an explicit ``False`` from |
| 50 | +any :meth:`start_x` method. |
| 51 | +
|
| 52 | +Examples |
| 53 | +-------- |
| 54 | +
|
| 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. |
| 58 | +
|
| 59 | +.. literalinclude:: /../../doc/api/code_examples/select_every_xth_test.py |
| 60 | +
|
| 61 | +For more examples it is possible to look at the source code of visitors used |
| 62 | +internally by Robot Framework itself. Some good examples are |
| 63 | +:class:`~robot.model.tagsetter.TagSetter` and |
| 64 | +:mod:`keyword removers <robot.result.keywordremover>`. |
| 65 | +""" |
| 66 | + |
| 67 | + |
15 | 68 | class SuiteVisitor(object): |
| 69 | + """Abstract class to ease traversing through the test suite structure. |
| 70 | +
|
| 71 | + See the :mod:`module level <robot.model.visitor>` documentation for more |
| 72 | + information and an example. |
| 73 | + """ |
16 | 74 |
|
17 | 75 | def visit_suite(self, suite): |
| 76 | + """Implements traversing through the suite and its direct children. |
| 77 | +
|
| 78 | + Can be overridden to allow modifying the passed in ``suite`` without |
| 79 | + calling :func:`start_suite` or :func:`end_suite` nor visiting child |
| 80 | + suites, tests or keywords (setup and teardown) at all. |
| 81 | + """ |
18 | 82 | if self.start_suite(suite) is not False: |
19 | 83 | suite.keywords.visit(self) |
20 | 84 | suite.suites.visit(self) |
21 | 85 | suite.tests.visit(self) |
22 | 86 | self.end_suite(suite) |
23 | 87 |
|
24 | 88 | def start_suite(self, suite): |
| 89 | + """Called when suite starts. Default implementation does nothing. |
| 90 | +
|
| 91 | + Can return explicit ``False`` to stop visiting. |
| 92 | + """ |
25 | 93 | pass |
26 | 94 |
|
27 | 95 | def end_suite(self, suite): |
| 96 | + """Called when suite ends. Default implementation does nothing.""" |
28 | 97 | pass |
29 | 98 |
|
30 | 99 | def visit_test(self, test): |
| 100 | + """Implements traversing through the test and its keywords. |
| 101 | +
|
| 102 | + Can be overridden to allow modifying the passed in ``test`` without |
| 103 | + calling :func:`start_test` or :func:`end_test` nor visiting keywords. |
| 104 | + """ |
31 | 105 | if self.start_test(test) is not False: |
32 | 106 | test.keywords.visit(self) |
33 | 107 | self.end_test(test) |
34 | 108 |
|
35 | 109 | def start_test(self, test): |
| 110 | + """Called when test starts. Default implementation does nothing. |
| 111 | +
|
| 112 | + Can return explicit ``False`` to stop visiting. |
| 113 | + """ |
36 | 114 | pass |
37 | 115 |
|
38 | 116 | def end_test(self, test): |
| 117 | + """Called when test ends. Default implementation does nothing.""" |
39 | 118 | pass |
40 | 119 |
|
41 | 120 | def visit_keyword(self, kw): |
| 121 | + """Implements traversing through the keyword and its child keywords. |
| 122 | +
|
| 123 | + Can be overridden to allow modifying the passed in ``kw`` without |
| 124 | + calling :func:`start_keyword` or :func:`end_keyword` nor visiting |
| 125 | + child keywords. |
| 126 | + """ |
42 | 127 | if self.start_keyword(kw) is not False: |
43 | 128 | kw.keywords.visit(self) |
44 | 129 | kw.messages.visit(self) |
45 | 130 | self.end_keyword(kw) |
46 | 131 |
|
47 | 132 | def start_keyword(self, keyword): |
| 133 | + """Called when keyword starts. Default implementation does nothing. |
| 134 | +
|
| 135 | + Can return explicit ``False`` to stop visiting. |
| 136 | + """ |
48 | 137 | pass |
49 | 138 |
|
50 | 139 | def end_keyword(self, keyword): |
| 140 | + """Called when keyword ends. Default implementation does nothing.""" |
51 | 141 | pass |
52 | 142 |
|
53 | 143 | def visit_message(self, msg): |
| 144 | + """Implements visiting the message. |
| 145 | +
|
| 146 | + Can be overridden to allow modifying the passed in ``msg`` without |
| 147 | + calling :func:`start_message` or :func:`end_message`. |
| 148 | + """ |
54 | 149 | if self.start_message(msg) is not False: |
55 | 150 | self.end_message(msg) |
56 | 151 |
|
57 | 152 | def start_message(self, msg): |
58 | | - pass |
59 | | - |
60 | | - def end_message(self, msg): |
61 | | - pass |
62 | | - |
63 | | - |
64 | | -class SkipAllVisitor(SuiteVisitor): |
65 | | - """Travels suite and it's sub-suites without doing anything.""" |
66 | | - def visit_suite(self, suite): |
67 | | - pass |
| 153 | + """Called when message starts. Default implementation does nothing. |
68 | 154 |
|
69 | | - def visit_keyword(self, kw): |
70 | | - pass |
71 | | - |
72 | | - def visit_test(self, test): |
| 155 | + Can return explicit ``False`` to stop visiting. |
| 156 | + """ |
73 | 157 | pass |
74 | 158 |
|
75 | | - def visit_message(self, msg): |
| 159 | + def end_message(self, msg): |
| 160 | + """Called when message ends. Default implementation does nothing.""" |
76 | 161 | pass |
0 commit comments