Robot Framework Assertion Engine

4.0.0 · active · verified Sun Apr 12

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

Install

Imports

Quickstart

This quickstart demonstrates how to integrate `robotframework-assertion-engine` into a custom Python library for Robot Framework. The `MyAssertions` class instantiates `AssertionEngine` and exposes keywords like `My Custom Assert Keyword` and `Verify Length` that utilize the engine's `verify_assertion` method. This allows library developers to easily add a wide range of assertions directly within their Python keywords, which can then be called from Robot Framework test cases.

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

view raw JSON →