robot.model package

Package with generic, reusable and extensible model classes.

This package contains, for example, TestSuite, TestCase, Keyword and SuiteVisitor base classes. These classes are extended both by execution and result related model objects and used also elsewhere.

This package is considered stable.

Submodules

robot.model.configurer module

class robot.model.configurer.SuiteConfigurer(name=None, doc=None, metadata=None, set_tags=None, include_tags=None, exclude_tags=None, include_suites=None, include_tests=None, empty_suite_ok=False)[source]

Bases: robot.model.visitor.SuiteVisitor

add_tags
remove_tags
visit_suite(suite)[source]
end_keyword(keyword)

Called when keyword ends. Default implementation does nothing.

end_message(msg)

Called when message ends. Default implementation does nothing.

end_suite(suite)

Called when suite ends. Default implementation does nothing.

end_test(test)

Called when test ends. Default implementation does nothing.

start_keyword(keyword)

Called when keyword starts. Default implementation does nothing.

Can return explicit False to stop visiting.

start_message(msg)

Called when message starts. Default implementation does nothing.

Can return explicit False to stop visiting.

start_suite(suite)

Called when suite starts. Default implementation does nothing.

Can return explicit False to stop visiting.

start_test(test)

Called when test starts. Default implementation does nothing.

Can return explicit False to stop visiting.

visit_keyword(kw)

Implements traversing through the keyword and its child keywords.

Can be overridden to allow modifying the passed in kw without calling start_keyword() or end_keyword() nor visiting child keywords.

visit_message(msg)

Implements visiting the message.

Can be overridden to allow modifying the passed in msg without calling start_message() or end_message().

visit_test(test)

Implements traversing through the test and its keywords.

Can be overridden to allow modifying the passed in test without calling start_test() or end_test() nor visiting keywords.

robot.model.criticality module

class robot.model.criticality.Criticality(critical_tags=None, non_critical_tags=None)[source]

Bases: object

tag_is_critical(tag)[source]
tag_is_non_critical(tag)[source]
test_is_critical(test)[source]

robot.model.filter module

class robot.model.filter.EmptySuiteRemover[source]

Bases: robot.model.visitor.SuiteVisitor

end_suite(suite)[source]
visit_test(test)[source]
visit_keyword(kw)[source]
end_keyword(keyword)

Called when keyword ends. Default implementation does nothing.

end_message(msg)

Called when message ends. Default implementation does nothing.

end_test(test)

Called when test ends. Default implementation does nothing.

start_keyword(keyword)

Called when keyword starts. Default implementation does nothing.

Can return explicit False to stop visiting.

start_message(msg)

Called when message starts. Default implementation does nothing.

Can return explicit False to stop visiting.

start_suite(suite)

Called when suite starts. Default implementation does nothing.

Can return explicit False to stop visiting.

start_test(test)

Called when test starts. Default implementation does nothing.

Can return explicit False to stop visiting.

visit_message(msg)

Implements visiting the message.

Can be overridden to allow modifying the passed in msg without calling start_message() or end_message().

visit_suite(suite)

Implements traversing through the suite and its direct children.

Can be overridden to allow modifying the passed in suite without calling start_suite() or end_suite() nor visiting child suites, tests or keywords (setup and teardown) at all.

class robot.model.filter.Filter(include_suites=None, include_tests=None, include_tags=None, exclude_tags=None)[source]

Bases: robot.model.filter.EmptySuiteRemover

include_suites
include_tests
include_tags
exclude_tags
start_suite(suite)[source]
end_keyword(keyword)

Called when keyword ends. Default implementation does nothing.

end_message(msg)

Called when message ends. Default implementation does nothing.

end_suite(suite)
end_test(test)

Called when test ends. Default implementation does nothing.

start_keyword(keyword)

Called when keyword starts. Default implementation does nothing.

Can return explicit False to stop visiting.

start_message(msg)

Called when message starts. Default implementation does nothing.

Can return explicit False to stop visiting.

start_test(test)

Called when test starts. Default implementation does nothing.

Can return explicit False to stop visiting.

visit_keyword(kw)
visit_message(msg)

Implements visiting the message.

Can be overridden to allow modifying the passed in msg without calling start_message() or end_message().

visit_suite(suite)

Implements traversing through the suite and its direct children.

Can be overridden to allow modifying the passed in suite without calling start_suite() or end_suite() nor visiting child suites, tests or keywords (setup and teardown) at all.

visit_test(test)

robot.model.imports module

class robot.model.imports.Import(type, name, args=(), alias=None, source=None)[source]

Bases: object

ALLOWED_TYPES = ('Library', 'Resource', 'Variables')
directory
report_invalid_syntax(message, level='ERROR')[source]
class robot.model.imports.Imports(source, imports=None)[source]

Bases: robot.model.itemlist.ItemList

library(name, args=(), alias=None)[source]
resource(path)[source]
variables(path, args=())[source]
append(item)
clear()
create(*args, **kwargs)
extend(items)
index(item, *start_and_end)
insert(index, item)
pop(*index)
visit(visitor)

robot.model.itemlist module

class robot.model.itemlist.ItemList(item_class, common_attrs=None, items=None)[source]

Bases: object

create(*args, **kwargs)[source]
append(item)[source]
extend(items)[source]
insert(index, item)[source]
pop(*index)[source]
index(item, *start_and_end)[source]
clear()[source]
visit(visitor)[source]

robot.model.keyword module

class robot.model.keyword.Keyword(name='', doc='', args=(), assign=(), tags=(), timeout=None, type='kw')[source]

Bases: robot.model.modelobject.ModelObject

Base model for a single keyword.

Extended by robot.running.model.Keyword and robot.result.model.Keyword.

KEYWORD_TYPE = 'kw'

Normal keyword type.

SETUP_TYPE = 'setup'

Setup type.

TEARDOWN_TYPE = 'teardown'

Teardown type.

FOR_LOOP_TYPE = 'for'

For loop type.

FOR_ITEM_TYPE = 'foritem'

Single for loop iteration type.

keyword_class = None

Internal usage only.

message_class

Internal usage only.

alias of Message

doc
args

Keyword arguments as a list of strings.

assign

Assigned variables as a list of strings.

timeout
type

Keyword type as a string. The value is either KEYWORD_TYPE, SETUP_TYPE, TEARDOWN_TYPE, FOR_LOOP_TYPE or FOR_ITEM_TYPE constant defined on the class level.

name
parent

Parent test suite, test case or keyword.

tags

Keyword tags as a Tags object.

keywords

Child keywords as a Keywords object.

messages

Messages as a Messages object.

children

Child keywords and messages in creation order.

id

Keyword id in format like s1-t3-k1.

See TestSuite.id for more information.

visit(visitor)[source]

Visitor interface entry-point.

class robot.model.keyword.Keywords(keyword_class=<class 'robot.model.keyword.Keyword'>, parent=None, keywords=None)[source]

Bases: robot.model.itemlist.ItemList

A list-like object representing keywords in a suite, a test or a keyword.

Possible setup and teardown keywords are directly available as setup and teardown attributes.

append(item)
clear()
create(*args, **kwargs)
extend(items)
index(item, *start_and_end)
insert(index, item)
pop(*index)
visit(visitor)
setup

Keyword used as the setup or None if no setup.

Can be set to a new setup keyword or None since RF 3.0.1.

teardown

Keyword used as the teardown or None if no teardown.

Can be set to a new teardown keyword or None since RF 3.0.1.

all

Iterates over all keywords, including setup and teardown.

normal

Iterates over normal keywords, omitting setup and teardown.

robot.model.message module

class robot.model.message.Message(message='', level='INFO', html=False, timestamp=None, parent=None)[source]

Bases: robot.model.modelobject.ModelObject

A message created during the test execution.

Can be a log message triggered by a keyword, or a warning or an error that occurred during parsing or test execution.

message

The message content as a string.

level

Severity of the message. Either TRACE, DEBUG, INFO, WARN, ERROR, or FAIL. The latest one is only used with keyword failure messages.

html

True if the content is in HTML, False otherwise.

timestamp

Timestamp in format %Y%m%d %H:%M:%S.%f.

parent

The object this message was triggered by.

html_message

Returns the message content as HTML.

visit(visitor)[source]

Visitor interface entry-point.

class robot.model.message.Messages(message_class=<class 'robot.model.message.Message'>, parent=None, messages=None)[source]

Bases: robot.model.itemlist.ItemList

append(item)
clear()
create(*args, **kwargs)
extend(items)
index(item, *start_and_end)
insert(index, item)
pop(*index)
visit(visitor)

robot.model.metadata module

class robot.model.metadata.Metadata(initial=None)[source]

Bases: robot.utils.normalizing.NormalizedDict

clear()
copy()
get(k[, d]) → D[k] if k in D, else d. d defaults to None.
items() → list of D's (key, value) pairs, as 2-tuples
iteritems() → an iterator over the (key, value) items of D
iterkeys() → an iterator over the keys of D
itervalues() → an iterator over the values of D
keys() → list of D's keys
pop(k[, d]) → v, remove specified key and return the corresponding value.

If key is not found, d is returned if given, otherwise KeyError is raised.

popitem() → (k, v), remove and return some (key, value) pair

as a 2-tuple; but raise KeyError if D is empty.

setdefault(k[, d]) → D.get(k,d), also set D[k]=d if k not in D
update([E, ]**F) → None. Update D from mapping/iterable E and F.

If E present and has a .keys() method, does: for k in E: D[k] = E[k] If E present and lacks .keys() method, does: for (k, v) in E: D[k] = v In either case, this is followed by: for k, v in F.items(): D[k] = v

values() → list of D's values

robot.model.modelobject module

class robot.model.modelobject.ModelObject[source]

Bases: object

robot.model.modifier module

class robot.model.modifier.ModelModifier(visitors, empty_suite_ok, logger)[source]

Bases: robot.model.visitor.SuiteVisitor

visit_suite(suite)[source]
end_keyword(keyword)

Called when keyword ends. Default implementation does nothing.

end_message(msg)

Called when message ends. Default implementation does nothing.

end_suite(suite)

Called when suite ends. Default implementation does nothing.

end_test(test)

Called when test ends. Default implementation does nothing.

start_keyword(keyword)

Called when keyword starts. Default implementation does nothing.

Can return explicit False to stop visiting.

start_message(msg)

Called when message starts. Default implementation does nothing.

Can return explicit False to stop visiting.

start_suite(suite)

Called when suite starts. Default implementation does nothing.

Can return explicit False to stop visiting.

start_test(test)

Called when test starts. Default implementation does nothing.

Can return explicit False to stop visiting.

visit_keyword(kw)

Implements traversing through the keyword and its child keywords.

Can be overridden to allow modifying the passed in kw without calling start_keyword() or end_keyword() nor visiting child keywords.

visit_message(msg)

Implements visiting the message.

Can be overridden to allow modifying the passed in msg without calling start_message() or end_message().

visit_test(test)

Implements traversing through the test and its keywords.

Can be overridden to allow modifying the passed in test without calling start_test() or end_test() nor visiting keywords.

robot.model.namepatterns module

class robot.model.namepatterns.SuiteNamePatterns(patterns=None)[source]

Bases: robot.model.namepatterns._NamePatterns

match(name, longname=None)
class robot.model.namepatterns.TestNamePatterns(patterns=None)[source]

Bases: robot.model.namepatterns._NamePatterns

match(name, longname=None)

robot.model.statistics module

class robot.model.statistics.Statistics(suite, suite_stat_level=-1, tag_stat_include=None, tag_stat_exclude=None, tag_stat_combine=None, tag_doc=None, tag_stat_link=None)[source]

Bases: object

Container for total, suite and tag statistics.

Accepted parameters have the same semantics as the matching command line options.

total = None

Instance of TotalStatistics.

suite = None

Instance of SuiteStatistics.

tags = None

Instance of TagStatistics.

visit(visitor)[source]
class robot.model.statistics.StatisticsBuilder(total_builder, suite_builder, tag_builder)[source]

Bases: robot.model.visitor.SuiteVisitor

start_suite(suite)[source]
end_suite(suite)[source]
visit_test(test)[source]
visit_keyword(kw)[source]
end_keyword(keyword)

Called when keyword ends. Default implementation does nothing.

end_message(msg)

Called when message ends. Default implementation does nothing.

end_test(test)

Called when test ends. Default implementation does nothing.

start_keyword(keyword)

Called when keyword starts. Default implementation does nothing.

Can return explicit False to stop visiting.

start_message(msg)

Called when message starts. Default implementation does nothing.

Can return explicit False to stop visiting.

start_test(test)

Called when test starts. Default implementation does nothing.

Can return explicit False to stop visiting.

visit_message(msg)

Implements visiting the message.

Can be overridden to allow modifying the passed in msg without calling start_message() or end_message().

visit_suite(suite)

Implements traversing through the suite and its direct children.

Can be overridden to allow modifying the passed in suite without calling start_suite() or end_suite() nor visiting child suites, tests or keywords (setup and teardown) at all.

robot.model.stats module

class robot.model.stats.Stat(name)[source]

Bases: robot.utils.sortable.Sortable

Generic statistic object used for storing all the statistic values.

name = None

Human readable identifier of the object these statistics belong to. Either All Tests or Critical Tests for TotalStatistics, long name of the suite for SuiteStatistics or name of the tag for TagStatistics

passed = None

Number of passed tests.

failed = None

Number of failed tests.

elapsed = None

Number of milliseconds it took to execute.

get_attributes(include_label=False, include_elapsed=False, exclude_empty=True, values_as_strings=False, html_escape=False)[source]
total
add_test(test)[source]
visit(visitor)[source]
class robot.model.stats.TotalStat(name)[source]

Bases: robot.model.stats.Stat

Stores statistic values for a test run.

type = 'total'
add_test(test)
get_attributes(include_label=False, include_elapsed=False, exclude_empty=True, values_as_strings=False, html_escape=False)
total
visit(visitor)
class robot.model.stats.SuiteStat(suite)[source]

Bases: robot.model.stats.Stat

Stores statistics values for a single suite.

type = 'suite'
id = None

Identifier of the suite, e.g. s1-s2.

elapsed = None

Number of milliseconds it took to execute this suite, including sub-suites.

add_stat(other)[source]
add_test(test)
get_attributes(include_label=False, include_elapsed=False, exclude_empty=True, values_as_strings=False, html_escape=False)
total
visit(visitor)
class robot.model.stats.TagStat(name, doc='', links=None, critical=False, non_critical=False, combined=None)[source]

Bases: robot.model.stats.Stat

Stores statistic values for a single tag.

type = 'tag'
doc = None

Documentation of tag as a string.

List of tuples in which the first value is the link URL and the second is the link title. An empty list by default.

critical = None

True if tag is considered critical, False otherwise.

non_critical = None

True if tag is considered non-critical, False otherwise.

combined = None

Pattern as a string if the tag is combined, None otherwise.

info

Returns additional information of the tag statistics are about. Either critical, non-critical, combined or an empty string.

add_test(test)
get_attributes(include_label=False, include_elapsed=False, exclude_empty=True, values_as_strings=False, html_escape=False)
total
visit(visitor)
class robot.model.stats.CombinedTagStat(pattern, name=None, doc='', links=None)[source]

Bases: robot.model.stats.TagStat

match(tags)[source]
add_test(test)
get_attributes(include_label=False, include_elapsed=False, exclude_empty=True, values_as_strings=False, html_escape=False)
info

Returns additional information of the tag statistics are about. Either critical, non-critical, combined or an empty string.

total
type = 'tag'
visit(visitor)
class robot.model.stats.CriticalTagStat(tag_pattern, name=None, critical=True, doc='', links=None)[source]

Bases: robot.model.stats.TagStat

match(tags)[source]
add_test(test)
get_attributes(include_label=False, include_elapsed=False, exclude_empty=True, values_as_strings=False, html_escape=False)
info

Returns additional information of the tag statistics are about. Either critical, non-critical, combined or an empty string.

total
type = 'tag'
visit(visitor)

robot.model.suitestatistics module

class robot.model.suitestatistics.SuiteStatistics(suite)[source]

Bases: object

Container for suite statistics.

stat = None

Instance of SuiteStat.

suites = None

List of TestSuite objects.

visit(visitor)[source]
class robot.model.suitestatistics.SuiteStatisticsBuilder(suite_stat_level)[source]

Bases: object

current
start_suite(suite)[source]
add_test(test)[source]
end_suite()[source]

robot.model.tags module

class robot.model.tags.Tags(tags=None)[source]

Bases: object

add(tags)[source]
remove(tags)[source]
match(tags)[source]
class robot.model.tags.TagPatterns(patterns)[source]

Bases: object

match(tags)[source]
robot.model.tags.TagPattern(pattern)[source]
class robot.model.tags.SingleTagPattern(pattern)[source]

Bases: object

match(tags)[source]
class robot.model.tags.AndTagPattern(patterns)[source]

Bases: object

match(tags)[source]
class robot.model.tags.OrTagPattern(patterns)[source]

Bases: object

match(tags)[source]
class robot.model.tags.NotTagPattern(must_match, *must_not_match)[source]

Bases: object

match(tags)[source]

robot.model.tagsetter module

class robot.model.tagsetter.TagSetter(add=None, remove=None)[source]

Bases: robot.model.visitor.SuiteVisitor

start_suite(suite)[source]
visit_test(test)[source]
visit_keyword(keyword)[source]
end_keyword(keyword)

Called when keyword ends. Default implementation does nothing.

end_message(msg)

Called when message ends. Default implementation does nothing.

end_suite(suite)

Called when suite ends. Default implementation does nothing.

end_test(test)

Called when test ends. Default implementation does nothing.

start_keyword(keyword)

Called when keyword starts. Default implementation does nothing.

Can return explicit False to stop visiting.

start_message(msg)

Called when message starts. Default implementation does nothing.

Can return explicit False to stop visiting.

start_test(test)

Called when test starts. Default implementation does nothing.

Can return explicit False to stop visiting.

visit_message(msg)

Implements visiting the message.

Can be overridden to allow modifying the passed in msg without calling start_message() or end_message().

visit_suite(suite)

Implements traversing through the suite and its direct children.

Can be overridden to allow modifying the passed in suite without calling start_suite() or end_suite() nor visiting child suites, tests or keywords (setup and teardown) at all.

robot.model.tagstatistics module

class robot.model.tagstatistics.TagStatistics(critical_stats, non_critical_stats, combined_stats)[source]

Bases: object

Container for tag statistics.

tags = None

Dictionary, where key is the name of the tag as a string and value is an instance of TagStat.

critical = None

List of CriticalTagStat objects.

non_critical = None

List of CriticalTagStat objects.

combined = None

List of CombinedTagStat objects.

visit(visitor)[source]
class robot.model.tagstatistics.TagStatisticsBuilder(criticality=None, included=None, excluded=None, combined=None, docs=None, links=None)[source]

Bases: object

add_test(test)[source]
class robot.model.tagstatistics.TagStatInfo(docs=None, links=None)[source]

Bases: object

get_stat(tag)[source]
get_critical_stats(criticality, critical=True)[source]
get_combined_stats(combined=None)[source]
get_doc(tag)[source]
class robot.model.tagstatistics.TagStatDoc(pattern, doc)[source]

Bases: object

match(tag)[source]

Bases: object

match(tag)[source]

robot.model.testcase module

class robot.model.testcase.TestCase(name='', doc='', tags=None, timeout=None)[source]

Bases: robot.model.modelobject.ModelObject

Base model for a single test case.

Extended by robot.running.model.TestCase and robot.result.model.TestCase.

keyword_class

Internal usage only

alias of Keyword

parent

Parent suite.

name

Test case name.

doc

Test case documentation.

timeout

Test case timeout.

tags

Test tags as a Tags object.

keywords

Keywords as a Keywords object.

Contains also possible setup and teardown keywords.

id

Test case id in format like s1-t3.

See TestSuite.id for more information.

longname

Test name prefixed with the long name of the parent suite.

visit(visitor)[source]

Visitor interface entry-point.

class robot.model.testcase.TestCases(test_class=<class 'robot.model.testcase.TestCase'>, parent=None, tests=None)[source]

Bases: robot.model.itemlist.ItemList

append(item)
clear()
create(*args, **kwargs)
extend(items)
index(item, *start_and_end)
insert(index, item)
pop(*index)
visit(visitor)

robot.model.testsuite module

class robot.model.testsuite.TestSuite(name='', doc='', metadata=None, source=None)[source]

Bases: robot.model.modelobject.ModelObject

Base model for single suite.

Extended by robot.running.model.TestSuite and robot.result.model.TestSuite.

test_class

Internal usage only.

alias of TestCase

keyword_class

Internal usage only.

alias of Keyword

parent

Parent suite. None with the root suite.

doc

Test suite documentation.

source

Path to the source file or directory.

name

Test suite name. If not set, constructed from child suite names.

longname

Suite name prefixed with the long name of the parent suite.

metadata

Free test suite metadata as a dictionary.

suites

Child suites as a TestSuites object.

tests

Tests as a TestCases object.

keywords

Suite setup and teardown as a Keywords object.

id

An automatically generated unique id.

The root suite has id s1, its child suites have ids s1-s1, s1-s2, ..., their child suites get ids s1-s1-s1, s1-s1-s2, ..., s1-s2-s1, ..., and so on.

The first test in a suite has an id like s1-t1, the second has an id s1-t2, and so on. Similarly keywords in suites (setup/teardown) and in tests get ids like s1-k1, s1-t1-k1, and s1-s4-t2-k5.

test_count

Number of the tests in this suite, recursively.

set_tags(add=None, remove=None, persist=False)[source]

Add and/or remove specified tags to the tests in this suite.

Parameters:
  • add – Tags to add as a list or, if adding only one, as a single string.
  • remove – Tags to remove as a list or as a single string. Can be given as patterns where * and ? work as wildcards.
  • persist – Add/remove specified tags also to new tests added to this suite in the future.
filter(included_suites=None, included_tests=None, included_tags=None, excluded_tags=None)[source]

Select test cases and remove others from this suite.

Parameters have the same semantics as --suite, --test, --include, and --exclude command line options. All of them can be given as a list of strings, or when selecting only one, as a single string.

Child suites that contain no tests after filtering are automatically removed.

Example:

suite.filter(included_tests=['Test 1', '* Example'],
             included_tags='priority-1')
configure(**options)[source]

A shortcut to configure a suite using one method call.

Parameters:options – Passed to SuiteConfigurer that will then set suite attributes, call filter(), etc. as needed.
remove_empty_suites()[source]

Removes all child suites not containing any tests, recursively.

visit(visitor)[source]

Visitor interface entry-point.

class robot.model.testsuite.TestSuites(suite_class=<class 'robot.model.testsuite.TestSuite'>, parent=None, suites=None)[source]

Bases: robot.model.itemlist.ItemList

append(item)
clear()
create(*args, **kwargs)
extend(items)
index(item, *start_and_end)
insert(index, item)
pop(*index)
visit(visitor)

robot.model.totalstatistics module

class robot.model.totalstatistics.TotalStatistics[source]

Bases: object

Container for total statistics.

critical = None

Instance of TotalStat for critical tests.

all = None

Instance of TotalStat for all the tests.

visit(visitor)[source]
message

String representation of the statistics.

For example:

2 critical tests, 1 passed, 1 failed
2 tests total, 1 passed, 1 failed
class robot.model.totalstatistics.TotalStatisticsBuilder(suite=None)[source]

Bases: robot.model.visitor.SuiteVisitor

add_test(test)[source]
visit_test(test)[source]
visit_keyword(kw)[source]
end_keyword(keyword)

Called when keyword ends. Default implementation does nothing.

end_message(msg)

Called when message ends. Default implementation does nothing.

end_suite(suite)

Called when suite ends. Default implementation does nothing.

end_test(test)

Called when test ends. Default implementation does nothing.

start_keyword(keyword)

Called when keyword starts. Default implementation does nothing.

Can return explicit False to stop visiting.

start_message(msg)

Called when message starts. Default implementation does nothing.

Can return explicit False to stop visiting.

start_suite(suite)

Called when suite starts. Default implementation does nothing.

Can return explicit False to stop visiting.

start_test(test)

Called when test starts. Default implementation does nothing.

Can return explicit False to stop visiting.

visit_message(msg)

Implements visiting the message.

Can be overridden to allow modifying the passed in msg without calling start_message() or end_message().

visit_suite(suite)

Implements traversing through the suite and its direct children.

Can be overridden to allow modifying the passed in suite without calling start_suite() or end_suite() nor visiting child suites, tests or keywords (setup and teardown) at all.

robot.model.visitor module

Interface to ease traversing through a test suite structure.

Visitors make it easy to modify test suite structures or to collect information from them. They work both with the executable model and the result model, but the objects passed to the visitor methods are slightly different depending on the model they are used with. The main differences are that on the execution side keywords do not have child keywords nor messages, and that only the result objects have status related attributes like status and starttime.

This module contains SuiteVisitor that implements the core logic to visit a test suite structure, and the result package contains ResultVisitor that supports visiting the whole test execution result structure. Both of these visitors should be imported via the robot.api package when used by external code.

Visitor algorithm

All suite, test, keyword and message objects have a visit() method that accepts a visitor instance. These methods will then call the correct visitor method visit_suite(), visit_test(), visit_keyword() or visit_message(), depending on the instance where the visit() method exists.

The recommended and definitely easiest way to implement a visitor is extending the SuiteVisitor base class. The default implementation of its visit_x() methods take care of traversing child elements of the object x recursively. A visit_x() method first calls a corresponding start_x() method (e.g. visit_suite() calls start_suite()), then calls visit() for all child objects of the x object, and finally calls the corresponding end_x() method. The default implementations of start_x() and end_x() do nothing.

Visitors extending the SuiteVisitor can stop visiting at a certain level either by overriding suitable visit_x() method or by returning an explicit False from any start_x() method.

Examples

The following example visitor modifies the test suite structure it visits. It could be used, for example, with Robot Framework’s --prerunmodifier option to modify test data before execution.

from robot.api import SuiteVisitor


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

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

    def start_suite(self, suite):
        """Modify suite's tests to contain only every Xth."""
        suite.tests = suite.tests[self.start::self.x]

    def end_suite(self, suite):
        """Remove suites that are empty after removing tests."""
        suite.suites = [s for s in suite.suites if s.test_count > 0]

    def visit_test(self, test):
        """Save time to avoid visiting tests and their keywords."""
        pass

For more examples it is possible to look at the source code of visitors used internally by Robot Framework itself. Some good examples are TagSetter and keyword removers.

class robot.model.visitor.SuiteVisitor[source]

Bases: object

Abstract class to ease traversing through the test suite structure.

See the module level documentation for more information and an example.

visit_suite(suite)[source]

Implements traversing through the suite and its direct children.

Can be overridden to allow modifying the passed in suite without calling start_suite() or end_suite() nor visiting child suites, tests or keywords (setup and teardown) at all.

start_suite(suite)[source]

Called when suite starts. Default implementation does nothing.

Can return explicit False to stop visiting.

end_suite(suite)[source]

Called when suite ends. Default implementation does nothing.

visit_test(test)[source]

Implements traversing through the test and its keywords.

Can be overridden to allow modifying the passed in test without calling start_test() or end_test() nor visiting keywords.

start_test(test)[source]

Called when test starts. Default implementation does nothing.

Can return explicit False to stop visiting.

end_test(test)[source]

Called when test ends. Default implementation does nothing.

visit_keyword(kw)[source]

Implements traversing through the keyword and its child keywords.

Can be overridden to allow modifying the passed in kw without calling start_keyword() or end_keyword() nor visiting child keywords.

start_keyword(keyword)[source]

Called when keyword starts. Default implementation does nothing.

Can return explicit False to stop visiting.

end_keyword(keyword)[source]

Called when keyword ends. Default implementation does nothing.

visit_message(msg)[source]

Implements visiting the message.

Can be overridden to allow modifying the passed in msg without calling start_message() or end_message().

start_message(msg)[source]

Called when message starts. Default implementation does nothing.

Can return explicit False to stop visiting.

end_message(msg)[source]

Called when message ends. Default implementation does nothing.