Search code examples
robotframework

Build a Robot Framework list from several keyword calls


I have several Robot Framework keywords that return a basic string.

@keyword
def keyword_one():
    return 'one'

@keyword
def keyword_two():
    return 'two'

In a robot test case, I try to build a list with this items, but I can't figure out how to do that is one line.

*** Test Cases ***

Test Case List
    @{my_list}=   Create List   Keyword One    Keywork Two

I tried several syntax but can't make it work. Of course, something like below works (hardcoded values).

*** Test Cases ***

Test Case List
    @{my_list}=   Create List   one    two

Thanks for your help.


Solution

  • At the time that I write this, robot doesn't have the ability to call keywords inline, so what you want isn't directly possible.

    You could write your own keyword to do this, however. The keyword can accept multiple keywords as arguments, and use run keyword from the built-in library to run the keyword.

    For example, the following keyword definition creates a keyword that creates a list of results from multiple keywords:

    Keyword written in python

    If you want to try this out, name the file example.py

    from robot.libraries.BuiltIn import BuiltIn
    
    builtin = BuiltIn()
    
    def create_list_from_keywords(*keywords):
        result = []
        for keyword in keywords:
            result.append(builtin.run_keyword(keyword))
        return result
    

    Example test

    *** Settings ***
    Library  example.py
    
    *** Keywords ***
    Keyword one
        return from keyword  one
    
    Keyword two
        return from keyword  two
    
    *** Test cases ***
    Example
        @{actual}=    create list from keywords  Keyword one  Keyword two
        @{expected}=  create list                one          two
    
        Should be equal  ${actual}  ${expected}
    

    Robot-based keyword definition

    If you're uncomfortable with python, here's a robot-based keyword definition:

    Create list from keywords
        [Arguments]  @{keywords}
        [Return]     @{result}
    
        @{result}=   create list
        FOR  ${keyword}  IN  @{keywords}
            ${keyword result}=  Run keyword  ${keyword}
            Append to list  ${result}  ${keyword result}
        END