RenameVariables#

Rename and normalize variable names.

Enabling the transformer

RenameVariables is not included in default transformers, that’s why you need to call it with --transform explicitly:

robotidy --transform RenameVariables src

Or configure enabled parameter:

robotidy --configure RenameVariables:enabled=True

Variable names in Settings, Variables, Test Cases and Keywords section are renamed. Variables in arguments are also affected.

Following conventions are applied:

  • variable case depends on the variable scope (lowercase for local variables and uppercase for non-local variables)

  • leading and trailing whitespace is stripped

  • more than 2 consecutive whitespace in name is replaced by 1

  • whitespace is replaced by _

  • camelCase is converted to snake_case

Conventions can be configured or switched off using parameters - read more in the following sections.

*** Settings ***
Suite Setup    ${keyword}

*** Variables ***
${global}    String with {other global}

*** Test Cases ***
Test
    ${local}    Set Variable    variable
    Log    ${local}
    Log    ${global}
    Log    ${local['item']}

*** Keywords ***
Keyword
    [Arguments]    ${ARG}
    Log    ${arg}

Keyword With ${EMBEDDED}
    Log    ${emb   eded}
*** Settings ***
Suite Setup    ${KEYWORD}

*** Variables ***
${GLOBAL}    String with {OTHER_GLOBAL}

*** Test Cases ***
Test
    ${local}    Set Variable    variable
    Log    ${local}
    Log    ${GLOBAL}
    Log    ${local['item']}

*** Keywords ***
Keyword
    [Arguments]    ${arg}
    Log    ${arg}

Keyword With ${embedded}
    Log    ${emb_eded}

Note

RenameVariables is still under development and is not considered a complete feature. The following syntax is not yet supported:

  • variable evaluation with ${variable * 2} (following will be replaced to ${variable_*_2}

  • variables passed by variable, not value ($var) are ignored

Robotidy can be locally disabled with # robotidy: off if you want to ignore specific cases.

Variable case in Settings section#

All variables in the *** Settings *** section are formatted to uppercase. This behaviour is configurable using settings_section_case:

> robotidy -c RenameVariables:settings_section_case=upper src

Allowed values are:

  • upper (default) to uppercase names

  • lower to lowercase names

  • ignore to leave existing case

Variable case in Variables section#

All variables in the *** Variables *** section are formatted to uppercase. This behaviour is configurable using variables_section_case:

> robotidy -c RenameVariables:variables_section_case=upper src

Allowed values are:

  • upper (default) to uppercase names

  • lower to lowercase names

  • ignore to leave existing case

Variable case in Keywords, Tasks and Test Cases sections#

Variable case in *** Keywords ***, *** Tasks *** and *** Test Cases *** sections depends on the variable scope. Local variables are lowercase and global variables are uppercase. Any unknown variable (not defined in current keyword or test case) is considered as global. You can configure what happes with unknown variables using unknown_variables_case:

> robotidy -c RenameVariables:unknown_variables_case=upper src

Allowed values are:

  • upper (default) to uppercase unknown names

  • lower to lowercase unknown names

  • ignore to leave existing case

*** Keywords ***
Keyword
    [Arguments]    ${arg}  # ${arg} is known
    ${local}    Set Variable    value  # since we set it, ${local} is also known
    Keyword Call    ${arg}    ${local}    ${global}  # ${global} is unknown
*** Keywords ***
Keyword
    [Arguments]    ${arg}  # ${arg} is known
    ${local}    Set Variable    value  # since we set it, ${local} is also known
    Keyword Call    ${arg}    ${local}    ${GLOBAL}  # ${global} is unknown
*** Keywords ***
Keyword
    [Arguments]    ${arg}  # ${arg} is known
    ${local}    Set Variable    value  # since we set it, ${local} is also known
    Keyword Call    ${arg}    ${local}    ${global}  # ${global} is unknown
*** Keywords ***
Keyword
    [Arguments]    ${arg}  # ${arg} is known
    ${local}    Set Variable    value  # since we set it, ${local} is also known
    Keyword Call    ${arg}    ${local}    ${global}  # ${global} is unknown

Converting camelCase to snake_case#

Variable names written in camelCase are converted to snake_case. You can disable this behaviour by configuring convert_camel_case to False:

> robotidy -c RenameVariables:convert_camel_case=False
*** Variables ***
${camelCase}    value

*** Keywords ***
Keyword
    ${CamelCase_Name}    Set Variable    value
    Keyword Call    ${CamelCase_Name}
*** Variables ***
${camel_case}    value

*** Keywords ***
Keyword
    ${camel_case_name}    Set Variable    value
    Keyword Call    ${camel_case_name}
*** Variables ***
${CAMELCASE}    value

*** Keywords ***
Keyword
    ${camelcase_name}    Set Variable    value
    Keyword Call    ${camelcase_name}

Variable separator#

Separators inside variable name are converted to underscore (_). You can configure it using variable_separator:

> robotidy -c RenameVariables:variable_separator=underscore

Allowed values are:

  • underscore (default)

  • space

*** Variables ***
${camelCase}    value

*** Keywords ***
Keyword
    ${variable_name}    Set Variable    value
    Keyword Call    ${variable name}
*** Variables ***
${CAMEL_CASE}    value

*** Keywords ***
Keyword
    ${variable_name}    Set Variable    value
    Keyword Call    ${variable_name}
*** Variables ***
${CAMEL CASE}    value

*** Keywords ***
Keyword
    ${variable name}    Set Variable    value
    Keyword Call    ${variable name}

Skip formatting#

It is possible to use the following arguments to skip formatting of the code:

It is also possible to use disablers (Disablers) but skip option makes it easier to skip all instances of given type of the code.