Robot Framework
Robot Framework is a generic open-source automation framework for acceptance testing, acceptance test-driven development (ATDD), and robotic process automation (RPA). It uses a keyword-driven approach, allowing for easy-to-read test cases. The project is actively maintained with frequent bug fix releases (e.g., 7.4.x series) and feature releases (e.g., 7.4, 7.3) typically a few times a year. The current version is 7.4.2.
Warnings
- breaking Robot Framework 4.0 introduced significant breaking changes, notably the 'FOR' loop syntax changed from `:FOR` to `FOR`, and Python 2 support was completely dropped. All Robot Framework 4.x and newer versions require Python 3.
- deprecated The built-in `Testdoc` tool has been deprecated in Robot Framework 7.4.2 in favor of the external `robotframework-testdoc` tool.
- gotcha Robot Framework is primarily driven by its domain-specific language (DSL) in `.robot` files and executed via a command-line interface. It's not a 'pure Python' testing framework like `pytest` or `unittest`. While it has a Python API for extension (custom libraries, listeners), most test logic resides in `.robot` files.
- gotcha When creating custom Python libraries for Robot Framework, their modules must be discoverable. This often means ensuring they are in the `PYTHONPATH` or placed directly under the test suite directory.
- gotcha Robot Framework's variable scoping (scalar, list, dictionary, global, suite, test) can be complex and lead to unexpected behavior if not understood.
Install
-
pip install robotframework
Imports
- robot.run
from robot import run
- keyword
from robot.api.deco import keyword
- BuiltIn
from robot.libraries.BuiltIn import BuiltIn
Quickstart
import os
import tempfile
from pathlib import Path
from robot import run
# Create a temporary .robot test file
robot_content = '''
*** Settings ***
Library OperatingSystem
*** Test Cases ***
My First Robot Test
Log To Console Hello from Robot Framework!
Create File ${TEMPDIR}${/}robot_test.txt This is a test file.
File Should Exist ${TEMPDIR}${/}robot_test.txt
Remove File ${TEMPDIR}${/}robot_test.txt
'''
with tempfile.TemporaryDirectory() as tmpdir:
test_file_path = Path(tmpdir) / 'my_test.robot'
test_file_path.write_text(robot_content)
print(f"Running Robot Framework test from: {test_file_path}")
# Execute the test programmatically
# Setting loglevel to DEBUG for verbose output, can be omitted
result = run(test_file_path.as_posix(), loglevel='INFO', outputdir=tmpdir)
if result == 0:
print("Robot Framework test ran successfully!")
else:
print(f"Robot Framework test failed with exit code: {result}")
print(f"Output and logs can be found in: {tmpdir}")