Robotframework 机器人检索环境变量

Robotframework 机器人检索环境变量,robotframework,Robotframework,根据这里的文件 Get Environment Variable (name, default=None) Returns the value of an environment variable with the given name. If no such environment variable is set, returns the default value, if given. Otherwise fails the test case. Returned variab

根据这里的文件

Get Environment Variable    (name, default=None)    
Returns the value of an environment variable with the given name.
If no such environment variable is set, returns the default value, if given. Otherwise fails the test case.
Returned variables are automatically decoded to Unicode using the system encoding.
Note that you can also access environment variables directly using the variable syntax %{ENV_VAR_NAME}.
我应该可以使用

${env\u var}=Get环境变量STAGING\u SERVER
登录到控制台${env_var}

但Robot一直在抱怨没有这样的变量。但当我切换到终端并运行echo$STAGING\u服务器时,我得到了所需的输出


我能做什么?

您正在使用的终端中没有设置变量。 请参见Windows命令窗口中的此示例:

C:\Testes>echo %STAGING_SERVER%
%STAGING_SERVER%

C:\Testes>robot -t env_var Example.robot
==============================================================================
Example
==============================================================================
env var                                                               | FAIL |
Environment variable 'STAGING_SERVER' does not exist.
------------------------------------------------------------------------------
Example                                                               | FAIL |
1 critical test, 0 passed, 1 failed
1 test total, 0 passed, 1 failed
==============================================================================
Output:  C:\Testes\output.xml
Log:     C:\Testes\log.html
Report:  C:\Testes\report.html

C:\Testes>set STAGING_SERVER="The variable is set"

C:\Testes>robot -t env_var Example.robot
==============================================================================
Example
==============================================================================
env var                                                               ."The variable is set"
env var                                                               | PASS |
------------------------------------------------------------------------------
Example                                                               | PASS |
1 critical test, 1 passed, 0 failed
1 test total, 1 passed, 0 failed
==============================================================================
Output:  C:\Testes\output.xml
Log:     C:\Testes\log.html
Report:  C:\Testes\report.html

C:\Testes>echo %STAGING_SERVER%
"The variable is set"
为了完整性,这是测试套件:

*** Settings ***
Library           OperatingSystem

*** Test Cases ***
env var
    ${env_var}=    Get Environment Variable    STAGING_SERVER
    Log To Console    ${env_var}


尝试使用“获取环境变量”检索所有可用环境变量的字典。查看STAGING_服务器是否列为密钥。@BrianO'Neill我运行了它,但它没有显示密钥。我使用
export
设置env变量。我需要使用其他东西吗?当你运行你的机器人测试时,你是在同一个终端上运行它吗?你是如何进行机器人测试的?也许它在一个没有环境变量的单独进程中运行。@Bryan Oakley我正在运行两个终端会话。一个是我运行Django development server,另一个是运行Robot测试。您确定环境变量在这两者中都设置了吗?您能在两个终端的外壳中看到环境变量吗?