'How to write Robotframework log and any keyword output in one line?

I want to print the output of any built-in or custom keyword using Log command in one line. for eg.

Log    Evaluate    1 + 2

This shows error that value of INFO for Log function is incorrect, i mean it assumes that Evaluate is parameter of Log function. I am aware that i can assign the value of Evaluate to a variable and then log that variable. But this adds multiple entries in log file. so just wondering if one liner is possible to do just as we do in python

def Evaluate(a, b):
   return a+b

print(Evaluate(1,2))


Solution 1:[1]

You can create a library with a keyword that will log the return value of Evaluate, but it will be the same as storing the return value of Evaluate in the Robot Framework script. So a one liner is possible ${retval}= Evaluate 1 + 2 but in general keywords are not substituted by their return value when passed as a parameter.

from robot.libraries.BuiltIn import BuiltIn

def evaluate_and_log_return_value(expression):
    retval= BuiltIn().evaluate(expression)
    BuiltIn().log(retval)

The test:

*** Settings ***
Library     var.py

*** Test Cases ***
Test 1
    Evaluate And Log Return Value    1 + 2
    
Test 2
    ${retval}=    Evaluate    1 + 2

You can check the output to see that basically the two are the same because variable assignments are logged by default. You do not have to call Log again if you are interested in the value of the assigned variable.

enter image description here

You can write a general keyword as well that will take any keyword by name with its arguments, it will execute it and log the return value.

from robot.libraries.BuiltIn import BuiltIn

def run_keyword_and_log_return_value(keyword, *args):
    BuiltIn().log(BuiltIn().run_keyword(keyword, *args))

Example:

*** Settings ***
Library     var.py

*** Test Cases ***
Test 3
    Run Keyword And Log Return Value    Evaluate    1 + 2
    Run Keyword And Log Return Value    Set Variable    data

Output:

enter image description here

Solution 2:[2]

In the specific case of logging the evaluate built in value, you can actually do inline evaluate since robotframewrok 3.2

log    ${{ 1 + 2 }}

As mentioned, this is limited to evaluate and cannot be used for other defined keyword result

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Jonatan Cloutier