Robot Framework Assertion Engine
Robot Framework Assertion Engine (version 4.0.0) provides a generic way to create meaningful and easy-to-use assertions for Robot Framework libraries. It is a spin-off from the Browser library project and offers a robust set of operators and formatters for in-keyword validation. The library is actively maintained with regular releases, including minor and major version updates to support new Robot Framework versions and introduce breaking changes.
Warnings
- breaking Version 3.0.0 introduced backwards incompatible changes to how scopes are defined. Scoping is now entirely managed by the library maintainer, and AssertionEngine no longer provides a keyword for scope definition.
- breaking Version 2.0.0 changed the logging of string error messages. Instead of plain text, strings are now converted using `repr()` to a printable representation, which includes Unicode values for whitespace characters. This can alter the appearance of error messages in logs.
- gotcha AssertionEngine v3.0.0 caused compatibility issues with `robotframework-browser` v16.0.0 (and Robot Framework 6.1.1) due to changes in the `Formatter()` constructor. This could lead to `TypeError: Formatter() takes no arguments` errors during library initialization.
- deprecated Python 3.7 support was dropped in version 1.1.0. The library currently requires Python 3.10 or newer.
Install
-
pip install robotframework-assertion-engine
Imports
- AssertionEngine
from AssertionEngine import AssertionEngine
- AssertionOperator
from AssertionEngine import AssertionOperator
Quickstart
import os
from AssertionEngine import AssertionEngine
class MyAssertions:
def __init__(self):
self._assertion_engine = AssertionEngine()
def my_custom_assert_keyword(self, actual_value, operator, expected_value, message=""):
"""
Performs an assertion using the AssertionEngine.
Example:
| My Custom Assert Keyword | hello | == | hello |
"""
print(f"Performing assertion: '{actual_value}' {operator} '{expected_value}'")
self._assertion_engine.verify_assertion(
actual_value=actual_value,
assertion_operator=operator,
assertion_expected=expected_value,
message=message
)
def verify_length(self, value, operator, expected_length):
"""
Verifies the length of a string or list.
Example:
| Verify Length | ${MY_LIST} | > | 3 |
"""
actual_length = len(value)
print(f"Verifying length of '{value}' (actual: {actual_length}) {operator} {expected_length}")
self._assertion_engine.verify_assertion(
actual_value=actual_length,
assertion_operator=operator,
assertion_expected=int(expected_length),
message=f"Length of '{value}'"
)
# To run with Robot Framework, save the above as 'MyAssertions.py'
# and create a .robot file like this:
#
# *** Settings ***
# Library MyAssertions.py
#
# *** Variables ***
# ${MY_LIST} ${{ [1, 2, 3, 4, 5] }}
#
# *** Test Cases ***
# Test String Equality
# My Custom Assert Keyword hello == hello
#
# Test String Inequality
# My Custom Assert Keyword world != robot
#
# Test Length Greater Than
# Verify Length ${MY_LIST} > 3
#
# Test Length Equal To
# Verify Length Python == 6