Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to have a variable span multiple lines in Robot Framework?

Tags:

I have a very long regex that I would like to put into a variable to test with. I'd like to be able to put it on multiple lines so that it's not so unreadable. I saw you could do multiple lines with the documentation tag. But when I try this formatting, Robot seems to think this is a list. Is there a way to do this in Robot Framework?

Consider:

${example_regex} =      '(?m)Setting IP address to [0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\nSetting MAC address to [0-9a-f]{2}:[0-9a-f]{2}:[0-9a-f]{2}:[0-9a-f]{2}:[0-9a-f]{2}:[0-9a-f]{2}\nSetting IP forwarding kernel options' 

I would like to be able to write:

${example_regex}   '(?m)Setting IP address to [0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\n                      Setting MAC address to [0-9a-f]{2}:[0-9a-f]{2}:[0-9a-f]{2}:[0-9a-f]{2}:[0-9a-f]{2}:[0-9a-f]{2}\n                      Setting IP forwarding kernel options' 
like image 345
siesta Avatar asked May 14 '13 19:05

siesta


People also ask

How do you continue to the next line in Robot Framework?

For those who want to have a newline, putting \n or ${\n} in the text works.

What are the 3 different types of variables in the robot framework?

There are three types of variables supported in robot framework − scalar, list and dictionary.

How do you use variable inside variables in robot framework?

Variables inside variables Variables are allowed also inside variables, and when this syntax is used, variables are resolved from the inside out. For example, if you have a variable ${var${x}}, then ${x} is resolved first. If it has the value name, the final value is then the value of the variable ${varname}.


2 Answers

In a Variables table

If you are creating the strings in a *** Variables *** table, you can spread the definition across multiple lines. You can use a special argument SEPARATOR to define how the cells are joined together. By default the lines are joined by a space, so you'll want to set it to the empty string by explicitly not giving SEPARATOR a value.

See Variable table in the user guide for more information.

*** Variables *** ${example_regex}=  SEPARATOR= ...  (?m)Setting IP address to [0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\n ...  Setting MAC address to [0-9a-f]{2}:[0-9a-f]{2}:[0-9a-f]{2}:[0-9a-f]{2}:[0-9a-f]{2}:[0-9a-f]{2}\\n ...  Setting IP forwarding kernel options 

In a test case or keyword

If you are trying to do this in a test case or keyword, you can't directly define a multiline string. However, you can get the same effect using the catenate keyword in a test case or keyword to join data which is spread across multiple cells. Be sure to properly escape your backslashes, and set the separator character to an empty string if you don't want newlines in the data.

*** Test Cases *** Multiline variable example   ${example_regex}=  catenate  SEPARATOR=   ...  (?m)Setting IP address to [0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\n   ...  Setting MAC address to [0-9a-f]{2}:[0-9a-f]{2}:[0-9a-f]{2}:[0-9a-f]{2}:[0-9a-f]{2}:[0-9a-f]{2}\\n   ...  Setting IP forwarding kernel options   log  regex: '${example_regex}' 
like image 118
Bryan Oakley Avatar answered Sep 22 '22 07:09

Bryan Oakley


Robot Framework 2.9 added support for multiline literal strings per the docs.

test.robot

*** Variables *** ${example_regex} =  SEPARATOR= ...  (?m)Setting IP address to [0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\n ...  Setting MAC address to [0-9a-f]{2}:[0-9a-f]{2}:[0-9a-f]{2}:[0-9a-f]{2}:[0-9a-f]{2}:[0-9a-f]{2}\\n ...  Setting IP forwarding kernel options  *** Test Cases *** Show output     Log  \n${example_regex}  console=yes 

robot test.robot

============================================================================== Test ============================================================================== Show output (?m)Setting IP address to [0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\nSetting MAC address to [0-9a-f]{2}:[0-9a-f]{2}:[0-9a-f]{2}:[0-9a-f]{2}:[0-9a-f]{2}:[0-9a-f]{2}\nSetting IP forwarding kernel options Show output                                                           | PASS | ------------------------------------------------------------------------------ Test                                                                  | PASS | 1 critical test, 1 passed, 0 failed 1 test total, 1 passed, 0 failed ============================================================================== 

A few notes:

  • All leading and trailing whitespace is trimmed from each line
  • A plain SEPARATOR= in the first line specifies no separator

You may also consider using variable files since then you get all the power of Python literal formatting, which can make maintaining things like complicated regular expressions easier. If you're using Robot Framework 3+ and Python 3.5+ (for f-strings) then it can look like:

vars.py

ip_address_pattern = r'[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}' lower_mac_address_pattern = '[0-9a-f]{2}:[0-9a-f]{2}:[0-9a-f]{2}:[0-9a-f]{2}:[0-9a-f]{2}:[0-9a-f]{2}' example_regex = (   rf'(?m)Setting IP address to {ip_address_pattern}\n'   rf'Setting MAC address to {lower_mac_address_pattern}\n'     'Setting IP forwarding kernel options' ) 

test.robot

*** Settings *** Variables  vars.py  *** Test Cases *** Show output     Log  \n${example_regex}  console=yes 

Which results in the same output as above.

like image 35
Chris Hunt Avatar answered Sep 22 '22 07:09

Chris Hunt