Skip to content

Commit 7a11c40

Browse files
committed
Merge branch 'prerun-modifier' of https://github.com/jaloren/robotframework into jaloren-prerun-modifier
2 parents 54738a0 + 6210a2f commit 7a11c40

File tree

4 files changed

+152
-11
lines changed

4 files changed

+152
-11
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ dist
55
MANIFEST
66
atest/results
77
utest/jasmine-results
8+
*~
89
.idea
910
.*project
1011
*.pyc

doc/api/code_examples/disable.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#!/usr/bin/python
2+
3+
from robot.api import SuiteVisitor
4+
import re
5+
6+
7+
class Setup(SuiteVisitor):
8+
"""Visitor that disables setup for suite or test case"""
9+
10+
def __init__(self, phase='all'):
11+
self.phase = phase
12+
13+
14+
def start_suite(self, suite):
15+
"""Remove setup for test suite"""
16+
if self.phase in ['suite','all']:
17+
suite.keywords.setup = None
18+
19+
def visit_test(self, test):
20+
"""Remove setup for test case"""
21+
if self.phase in ['test','all']:
22+
test.keywords.setup = None
23+
24+
25+
class Teardown(SuiteVisitor):
26+
"""Visitor that disables teardown for suite or test case"""
27+
28+
def __init__(self, phase='all'):
29+
self.phase = phase
30+
31+
32+
def start_suite(self, suite):
33+
"""Remove teardown for test suite"""
34+
if self.phase in ['suite','all']:
35+
suite.keywords.teardown = None
36+
37+
def visit_test(self, test):
38+
"""Remove teardown for test cases"""
39+
if self.phase in ['test','all']:
40+
test.keywords.teardown = None

doc/api/code_examples/filtertc.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
#!/usr/bin/python
2+
3+
from robot.api import SuiteVisitor
4+
import re
5+
6+
7+
class Tests(SuiteVisitor):
8+
"""Visitor that filters out test cases that would normally be executed"""
9+
10+
def __init__(self, search):
11+
self.search = search
12+
13+
14+
def start_suite(self, suite):
15+
"""Remove tests that match the string passed into constructor"""
16+
if len(suite.tests) < 1:
17+
return
18+
suite.tests = [t for t in suite.tests if self.search not in t.name]
19+
20+
21+
def end_suite(self, suite):
22+
"""Remove suites that are empty after removing tests."""
23+
suite.suites = [s for s in suite.suites if s.test_count > 0]
24+
25+
26+
def visit_test(self, test):
27+
"""Save time to avoid visiting tests and their keywords."""
28+
pass
29+
30+
31+
class Suites(SuiteVisitor):
32+
"""Visitor that filters out test cases that would normally be executed"""
33+
34+
def __init__(self, search):
35+
self.search = search
36+
37+
38+
def start_suite(self, suite):
39+
"""Remove tests whose parent or ancestor suites match the string passed into constructor"""
40+
if len(suite.tests) < 1:
41+
return
42+
test = suite.tests[0]
43+
suite_search = re.compile('(.*)(\.{0})'.format(test.name))
44+
suite_filter = suite_search.match(test.longname).group(1)
45+
suite.tests = [t for t in suite.tests if self.search not in suite_filter]
46+
47+
48+
def end_suite(self, suite):
49+
"""Remove suites that are empty after removing tests."""
50+
suite.suites = [s for s in suite.suites if s.test_count > 0]
51+
52+
53+
def visit_test(self, test):
54+
"""Save time to avoid visiting tests and their keywords."""
55+
pass

doc/userguide/src/ExecutingTestCases/ConfiguringExecution.rst

Lines changed: 56 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -515,13 +515,9 @@ the executable test suite structure and modify it as needed. The visitor
515515
interface is explained as part of the `Robot Framework API documentation
516516
<visitor interface_>`_, and it possible to modify executed `test suites
517517
<running.TestSuite_>`_, `test cases <running.TestCase_>`_ and `keywords
518-
<running.Keyword_>`_ using it. The example below ought to give an idea of
518+
<running.Keyword_>`_ using it. The examples below ought to give an idea of
519519
how model modifiers can be used and how powerful this functionality is.
520520

521-
.. sourcecode:: python
522-
523-
../api/code_examples/select_every_xth_test.py
524-
525521
When a model modifier is taken into use on the command line using the
526522
:option:`--prerunmodifier` option, it can be specified either as a name of
527523
the modifier class or a path to the modifier file. If the modifier is given
@@ -531,12 +527,25 @@ name must include both like `module.ModifierClass`. If the modifier is given
531527
as a path, the class name must be same as the file name. For most parts this
532528
works exactly like when `specifying a test library to import`__.
533529

534-
If a modifier requires arguments, like the example above does, they can be
530+
If a modifier requires arguments, like the examples below do, they can be
535531
specified after the modifier name or path using either a colon (`:`) or a
536532
semicolon (`;`) as a separator. If both are used in the value, the one first
537533
is considered the actual separator.
538534

539-
For example, if the above model modifier would be in a file
535+
If more than one model modifier is needed, they can be specified by using
536+
the :option:`--prerunmodifier` option multiple times. If similar modifying
537+
is needed before creating results, `programmatic modification of results`_
538+
can be enabled using the :option:`--prerebotmodifier` option.
539+
540+
Example: Select Every Xth Test
541+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
542+
In this code snippet, the prerun modifier causes the framework to skip over or filter out any test case that isn't Xth one, where Xth is a number passed in as an argument on the command line.
543+
544+
.. sourcecode:: python
545+
546+
../api/code_examples/select_every_xth_test.py
547+
548+
If the above model modifier is in a file
540549
:file:`SelectEveryXthTest.py`, it could be used like this::
541550

542551
# Specify the modifier as a path. Run every second test.
@@ -546,10 +555,46 @@ For example, if the above model modifier would be in a file
546555
# SelectEveryXthTest.py must be in the module search path.
547556
robot --prerunmodifier SelectEveryXthTest:3:1 tests.robot
548557

549-
If more than one model modifier is needed, they can be specified by using
550-
the :option:`--prerunmodifier` option multiple times. If similar modifying
551-
is needed before creating results, `programmatic modification of results`_
552-
can be enabled using the :option:`--prerebotmodifier` option.
558+
559+
Example: Filter Out Test Cases or Suite By Name
560+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
561+
The following prerun modifier removes any test case in which the user-defined string is found in the test name or in the test's parent or ancestor suites. This prerun modifier is like a negative version of the flags --suite and --test because test cases or test suites are excluded from the test run by name instead of tag.
562+
563+
.. sourcecode:: python
564+
565+
../api/code_examples/filtertc.py
566+
567+
Assuming the module that contained this prerun modifier is in the module search path and the module name is filtertc, then it could be used like this::
568+
569+
##Specify modifier by name where the namespace requires the module name since it differs from the class name.
570+
robot --prerunmodifier filtertc.Tests:'Create Table' --prerunmodifier filtertc.Suites:'database'
571+
572+
This means that any test case in any of the data sources that has the string 'Create Table' in the test name is excluded from the test run. In addition, any test case whose parent or ancester suites have the string database are also excluded from the test run.
573+
574+
575+
Example: Skip Setups and Teardowns
576+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
577+
When debugging test cases, a tester may need to temporarily disable a teardown or setup so that system under test is in a state that can be easily debugged. The following prerun modifier can disable setups and teardowns in either suites or test cases.
578+
579+
.. sourcecode:: python
580+
581+
../api/code_examples/disable.py
582+
583+
Assuming the module that contained this prerun modifier is in the module search path and the module name is disable, then execute this command to disable setup and teardowns for both suites and test cases.::
584+
585+
##Specify modifier by name where the namespace requires the module name since it differs from the class name.
586+
robot --prerunmodifier disable.Setup --prerunmodifier disable.Teardown
587+
588+
To disable just suite teardown and suite setup, execute this command:::
589+
590+
##Specify modifier by name where the namespace requires the module name since it differs from the class name.
591+
robot --prerunmodifier disable.Setup:suite --prerunmodifier disable.Teardown:suite
592+
593+
594+
To disable just test setup and test teardown, execute this command::
595+
596+
##Specify modifier by name where the namespace requires the module name since it differs from the class name.
597+
robot --prerunmodifier disable.Setup:test --prerunmodifier disable.Teardown:test
553598

554599
__ `Specifying library to import`_
555600

0 commit comments

Comments
 (0)