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:
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 loglibname(str): Name of the library owning the keywordstatus(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 loglibname(str): Name of the library owning the keywordstatus(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_keywordis 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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file robotframework_nestedlogger-1.0.3.tar.gz.
File metadata
- Download URL: robotframework_nestedlogger-1.0.3.tar.gz
- Upload date:
- Size: 8.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.10.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8a4071516900b6acc958e9e9c6e86018c6068d9c629f687269fa0bd90a3c48cf
|
|
| MD5 |
12fb3539799dfe9bb8451abc0b0afb98
|
|
| BLAKE2b-256 |
778f305c58fbc61e215a6125de6a0fe104abe1b2ef6a699dcba35b1d3f60ae68
|
File details
Details for the file robotframework_nestedlogger-1.0.3-py3-none-any.whl.
File metadata
- Download URL: robotframework_nestedlogger-1.0.3-py3-none-any.whl
- Upload date:
- Size: 8.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.10.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
df40346fff5ebb41872f34b841cc55d1ae2784cf25bec448a21435d450748c8c
|
|
| MD5 |
15c17b266d51a1f057ba2e88a0948662
|
|
| BLAKE2b-256 |
46ff00a9c89e3bdadb13ec0ff54021f6800fbc66f9f2ca1906630de156c13beb
|