Logs from Python in Robot Framework

Introduction

In this article, you can learn how to output messages from external Python libraries to the Robot Framework log.

It is assumed that you know the material of the article «Robot Framework Basics»

Let's consider a simple connection of an external library from the article «Architecture of tests on Robot Framework»

What should be done so that the function from unstable_url.py outputs something to the log file or to the robot console? The usual print() will not be visible anywhere.

from random import randrange def get_unstable_url() -> str: if randrange(2) == 1: return "https://www.urn.su" else: return "https://www.heihei.ru"

*** Settings *** Documentation Example that opens single page Library Browser ... enable_playwright_debug=${True} ... auto_closing_level=TEST ... retry_assertions_for=0:00:03 Library ../libraries/unstable_url.py Force Tags ui *** Variables *** *** Keywords *** Test Setup Tasks Start Chromium Browser Test Teardown Tasks Close Browser Start Chromium Browser New Browser browser=chromium headless=True New Context viewport={'width': 1920, 'height': 1080} ignoreHTTPSErrors=True *** Test Cases *** Starting a browser with a page [Tags] title ${url} = Get Unstable Url New Page ${url} Get Title == URN.SU Close Browser

BuitIn

from random import randrange from robot.libraries.BuiltIn import BuiltIn def write_to_console(s: str): BuiltIn().log_to_console(s) def get_unstable_url() -> str: if randrange(2) == 1: url = "https://www.urn.su" else: url = "https://www.heihei.ru" write_to_console(f"\nunstable url is: {url}\n") return url

============================================================================== Tests ============================================================================== Tests.Test Title :: Example that opens single page ============================================================================== Starting a browser with a page unstable url is: https://www.heihei.ru Starting a browser with a page | FAIL | Title 'HeiHei.ru' (str) should be 'URN.SU' (str) …

API

from random import randrange from robot.api import logger def write_to_console(s: str): logger.console(s) …

I do not recommend doing this in production, but you can override the function print()

from random import randrange from robot.api import logger # mode = "python" mode = "robot" if mode == "robot": def print(s: str): logger.console(s) def get_unstable_url() -> str: if randrange(2) == 1: url = "https://www.urn.su" else: url = "https://www.heihei.ru" print(f"\nunstable url is: {url}\n") return url

If you have many files in which you overload print(), I recommend using config

To send a message not to the console, but to the log, you need to replace console with the desired log level (info, warn, debug, or trace).

For example, to info

from random import randrange from robot.api import logger def write_to_console(s: str): logger.info(s) …

Robot www.aredel.com
INFO
log

Article author: Andrei Olegovich

Banner image