Generic automation framework for acceptance testing and robotic process automation (RPA)
—
Robot Framework's core execution APIs provide programmatic control over test execution, result processing, and output generation. These APIs enable integration with other tools and automation systems.
Execute Robot Framework tests programmatically with full control over execution options and settings.
def run(*tests, **options):
"""
Execute Robot Framework tests programmatically.
Args:
*tests: Test data sources (files, directories, or suites)
**options: Execution options (outputdir, loglevel, include, exclude, etc.)
Returns:
Result object with return_code attribute (0 for success)
"""
def run_cli(arguments=None, exit=True):
"""
Execute tests with command line argument processing.
Args:
arguments: List of command line arguments, defaults to sys.argv[1:]
exit: Whether to call sys.exit() with the result code
Returns:
Result object with return_code attribute
"""Usage Examples:
from robot import run
# Basic test execution
result = run('tests/acceptance.robot')
# Execute with options
result = run(
'tests/',
outputdir='results',
loglevel='DEBUG',
include=['smoke', 'critical'],
exclude=['slow', 'manual'],
variable=['ENV:production', 'VERSION:1.2.3'],
listener='MyListener.py'
)
# Check execution result
if result.return_code == 0:
print("All tests passed!")
else:
print(f"Some tests failed (return code: {result.return_code})")Process existing Robot Framework output files to generate new reports and logs with different settings.
def rebot(*outputs, **options):
"""
Post-process Robot Framework output files.
Args:
*outputs: Output XML files to process
**options: Processing options (outputdir, report, log, merge, etc.)
Returns:
Result object with return_code attribute
"""
def rebot_cli(arguments=None, exit=True):
"""
Post-process outputs with command line argument processing.
Args:
arguments: List of command line arguments, defaults to sys.argv[1:]
exit: Whether to call sys.exit() with the result code
Returns:
Result object with return_code attribute
"""Usage Examples:
from robot import rebot
# Merge multiple output files
result = rebot('output1.xml', 'output2.xml', 'output3.xml',
outputdir='merged_results',
name='Combined Test Results')
# Generate new reports with filtering
result = rebot('output.xml',
include=['critical'],
exclude=['known_issue'],
report='critical_report.html',
log='critical_log.html')Create and execute test suites programmatically without external test files.
class TestSuite:
"""
Executable test suite that can be created and run programmatically.
Attributes:
name: Suite name
doc: Suite documentation
metadata: Suite metadata dictionary
tests: Collection of test cases
keywords: Collection of suite keywords
setup: Suite setup keyword
teardown: Suite teardown keyword
"""
def __init__(self, name='', doc='', metadata=None, source=''): ...
def run(self, **options):
"""
Execute the test suite.
Args:
**options: Execution options (same as run() function)
Returns:
Result object with execution results
"""
class TestSuiteBuilder:
"""
Builder for creating test suites from file system.
"""
def __init__(self, include_extensions=('.robot', '.txt'), **options): ...
def build(self, *sources):
"""
Build test suite(s) from given sources.
Args:
*sources: File or directory paths
Returns:
TestSuite object or collection of suites
"""Usage Examples:
from robot.api import TestSuite
# Create a test suite programmatically
suite = TestSuite('My Test Suite', doc='Example test suite')
# Add metadata
suite.metadata['Version'] = '1.0'
suite.metadata['Environment'] = 'Test'
# Create test case
test = suite.tests.create('Verify Greeting')
test.keywords.create('Set Variable', args=['${greeting}', 'Hello World'])
test.keywords.create('Should Contain', args=['${greeting}', 'Hello'])
test.keywords.create('Log', args=['Test completed successfully'])
# Add suite setup and teardown
suite.setup.config(name='Log', args=['Starting test suite'])
suite.teardown.config(name='Log', args=['Test suite completed'])
# Run the suite
result = suite.run(outputdir='results', loglevel='INFO')Read and analyze Robot Framework execution results from XML output files.
def ExecutionResult(source, include_keywords=True, flattened=True):
"""
Factory function for creating execution result objects.
Args:
source: Path to output.xml file or file-like object
include_keywords: Whether to include keyword-level results
flattened: Whether to flatten structure
Returns:
Result object containing execution results
"""
class Result:
"""
Execution results read from output.xml file.
Attributes:
suite: Root test suite with all results
statistics: Test execution statistics
errors: Execution errors and messages
"""
@property
def suite(self): ...
@property
def statistics(self): ...
@property
def errors(self): ...
class ResultVisitor:
"""
Abstract base class for processing execution results.
Implement visit_* methods to process different result elements.
"""
def visit_result(self, result): ...
def visit_suite(self, suite): ...
def visit_test(self, test): ...
def visit_keyword(self, keyword): ...
def visit_message(self, message): ...Usage Examples:
from robot.api import ExecutionResult, ResultVisitor
# Read execution results
result = ExecutionResult('output.xml')
# Access basic statistics
print(f"Test suite: {result.suite.name}")
print(f"Total tests: {result.suite.test_count}")
print(f"Passed: {result.suite.statistics.passed}")
print(f"Failed: {result.suite.statistics.failed}")
# Iterate through test results
for test in result.suite.tests:
print(f"Test: {test.name}")
print(f"Status: {test.status}")
print(f"Message: {test.message}")
print(f"Start time: {test.start_time}")
print(f"End time: {test.end_time}")
print(f"Elapsed time: {test.elapsed_time}")
# Custom result processor
class TestReportGenerator(ResultVisitor):
def __init__(self):
self.failed_tests = []
def visit_test(self, test):
if not test.passed:
self.failed_tests.append({
'name': test.name,
'message': test.message,
'tags': list(test.tags)
})
# Process results with custom visitor
reporter = TestReportGenerator()
result.visit(reporter)
print(f"Failed tests: {len(reporter.failed_tests)}")Generate HTML reports and logs from execution results with customizable formatting and content.
class ResultWriter:
"""
Writes reports, logs, and other output files from execution results.
"""
def __init__(self, *sources): ...
def write_results(self, **options):
"""
Write result files (report, log, output XML, XUnit).
Args:
**options: Output options (report, log, output, xunit, etc.)
"""Usage Examples:
from robot.api import ResultWriter
# Generate reports from result object
writer = ResultWriter(result)
writer.write_results(
report='custom_report.html',
log='custom_log.html',
outputdir='custom_results',
title='Custom Test Report'
)
# Generate reports from XML files
writer = ResultWriter('output1.xml', 'output2.xml')
writer.write_results(
report='merged_report.html',
log='merged_log.html',
merge=True,
name='Merged Test Results'
)Process and modify test suites before execution using visitor pattern.
class SuiteVisitor:
"""
Abstract base class for processing test suites before execution.
Can be used with --prerunmodifier option.
"""
def start_suite(self, suite): ...
def end_suite(self, suite): ...
def start_test(self, test): ...
def end_test(self, test): ...
def start_keyword(self, keyword): ...
def end_keyword(self, keyword): ...
def visit_suite(self, suite): ...
def visit_test(self, test): ...
def visit_keyword(self, keyword): ...Usage Examples:
from robot.api import SuiteVisitor
class TagBasedFilter(SuiteVisitor):
"""Remove tests that don't have required tags."""
def __init__(self, required_tags):
self.required_tags = set(required_tags)
def start_suite(self, suite):
# Filter tests based on tags
suite.tests = [test for test in suite.tests
if self.required_tags.intersection(test.tags)]
def start_test(self, test):
# Add execution timestamp
test.tags.add(f"executed:{datetime.now().isoformat()}")
# Use with programmatic execution
modifier = TagBasedFilter(['smoke', 'critical'])
suite.visit(modifier)
result = suite.run()# Return codes
TestResult = int # 0 = success, >0 = failure/error count
# Execution statistics
class Statistics:
passed: int
failed: int
skipped: int
total: int
# Test status
TestStatus = Literal["PASS", "FAIL", "SKIP", "NOT RUN"]
# Time representation
TimeString = str # Format: "20240101 12:00:00.000"
TimeDelta = str # Format: "00:01:23.456"Install with Tessl CLI
npx tessl i tessl/pypi-robotframework