Skip to main content

A Robot Framework library for nested keyword logging in Python implementations

Project description

robotframework-NestedLogger

The goal of this library is to enable the registration of keywords so that they are visible at the HTML level as individual keywords, while their implementation is nested in python.

Description

The NestedLogger library allows Robot Framework test libraries to dynamically log keywords during test execution. This is particularly useful when you want to break down complex operations into smaller inside of python

Installation

Install from source

pip install .

Install in development mode

pip install -e .

Install from PyPI (when published)

pip install robotframework-nestedlogger

Usage

Basic Example (Traditional Way)

from NestedLogger import NestedLogger
from robot.api.deco import keyword

class MyLibrary:
        
    @keyword("Process Multiple Items")
    def process_items(self, *params_and_values):
        """ Fills form parameters with provided parameters and values.

        *Arguments:*
        | =Name= | =Description= | =Example value= |
        | params_and_values | Alternating parameter names and values | "Full Name"    "Artur Ziolkowski" |

        *Return*
        | String | Done |
        """
        my_logger = NestedLogger()

        lib_name = self.__class__.__name__
        for param, value in zip(params_and_values[::2], params_and_values[1::2]):
            kw_name = "Do operation for {param} with value {value}".format(param=param, value=value)
            my_logger.start_keyword(kw_name, lib_name)

            status = 'PASS'
            error = None
            try:
                print("do your code")
            except Exception as e:
                status = 'FAIL'
                error = e
            finally:
                my_logger.end_keyword(kw_name, lib_name, status)
                if error:
                    raise error

        return "Done"

    def _process_single_item(self, item):
        # Implementation
        print(f"Processing: {item}")

Using Context Manager (Recommended)

The NestedLogger class supports Python's context manager protocol, which provides cleaner code and automatic error handling:

from NestedLogger import NestedLogger
from robot.api.deco import keyword

class MyLibrary:
        
    @keyword("Process Multiple Items")
    def process_items(self, *params_and_values):
        """ Fills form parameters with provided parameters and values using context manager.

        *Arguments:*
        | =Name= | =Description= | =Example value= |
        | params_and_values | Alternating parameter names and values | "Full Name"    "Artur Ziolkowski" |

        *Return*
        | String | Done |
        """
        lib_name = self.__class__.__name__
        
        for param, value in zip(params_and_values[::2], params_and_values[1::2]):
            kw_name = "Do operation for {param} with value {value}".format(param=param, value=value)
            
            # Context manager automatically handles start/end and error status
            with NestedLogger(kw_name, lib_name, 'PASS'):
                print(f"Processing {param} with value {value}")
                # Your code here - if exception occurs, status will be automatically set to FAIL
                self._process_single_item(param, value)
                
        return "Done"

    def _process_single_item(self, param, value):
        # Implementation
        print(f"Processing parameter '{param}' with value '{value}'")

In Robot Framework Test

*** Settings ***
Library    MyLibrary

*** Test Cases ***
Test Processing
    Process Multiple Items    item1    item2    item3

Each item will appear as a separate keyword in the log.html report with its own pass/fail status.

Example Output

Here's how nested keywords appear in the Robot Framework log report:

Nested Keywords in Log Report

Each nested operation is logged as a separate keyword with its own pass/fail status, making it easy to trace execution and identify issues.

API

NestedLogger Class

Constructor: __init__(kwname=None, libname=None, status='PASS')

Creates a new NestedLogger instance.

Arguments:

  • kwname (str, optional): Name of the keyword to log (required for context manager usage)
  • libname (str, optional): Name of the library owning the keyword (required for context manager usage)
  • status (str, optional): Expected status for successful execution (default: 'PASS')

Example:

# For traditional usage
logger = NestedLogger()

# For context manager usage
with NestedLogger('My Keyword', 'MyLibrary', 'PASS'):
    # Your code here
    pass

start_keyword(kwname, libname, status='FAIL')

Starts logging a new nested keyword.

Arguments:

  • kwname (str): Name of the keyword to log
  • libname (str): Name of the library owning the keyword
  • status (str): Initial status (default: 'FAIL')

Example:

logger = NestedLogger()
logger.start_keyword('Process Item', 'MyLibrary')

end_keyword(kwname, libname, status)

Ends logging of a nested keyword.

Arguments:

  • kwname (str): Name of the keyword to log
  • libname (str): Name of the library owning the keyword
  • status (str): Final status ('PASS' or 'FAIL')

Example:

logger.end_keyword('Process Item', 'MyLibrary', 'PASS')

Context Manager Protocol

The NestedLogger class implements __enter__ and __exit__ methods, allowing it to be used with Python's with statement.

Benefits:

  • Automatic keyword start and end
  • Automatic error handling (sets status to 'FAIL' on exception)
  • Cleaner, more readable code
  • Ensures end_keyword is always called

Example:

with NestedLogger('My Operation', 'MyLibrary', 'PASS'):
    # Keyword automatically started
    do_something()
    # Keyword automatically ended with status 'PASS'
    # If exception occurs, status is automatically set to 'FAIL'

Requirements

  • Python >= 3.10
  • robotframework >= 7.3.0

License

This project is licensed under the Apache License 2.0 - see the LICENSE file for details.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

robotframework_nestedlogger-1.0.3.tar.gz (8.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

robotframework_nestedlogger-1.0.3-py3-none-any.whl (8.9 kB view details)

Uploaded Python 3

File details

Details for the file robotframework_nestedlogger-1.0.3.tar.gz.

File metadata

File hashes

Hashes for robotframework_nestedlogger-1.0.3.tar.gz
Algorithm Hash digest
SHA256 8a4071516900b6acc958e9e9c6e86018c6068d9c629f687269fa0bd90a3c48cf
MD5 12fb3539799dfe9bb8451abc0b0afb98
BLAKE2b-256 778f305c58fbc61e215a6125de6a0fe104abe1b2ef6a699dcba35b1d3f60ae68

See more details on using hashes here.

File details

Details for the file robotframework_nestedlogger-1.0.3-py3-none-any.whl.

File metadata

File hashes

Hashes for robotframework_nestedlogger-1.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 df40346fff5ebb41872f34b841cc55d1ae2784cf25bec448a21435d450748c8c
MD5 15c17b266d51a1f057ba2e88a0948662
BLAKE2b-256 46ff00a9c89e3bdadb13ec0ff54021f6800fbc66f9f2ca1906630de156c13beb

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page