Robot Framework测试用例间及跨文件夹全局变量使用问题
Robot Framework 测试用例间变量共享解决方案
一、同一文件内测试用例间变量共享
套件级变量(Suite Variable)默认作用域为当前测试文件,只要确保设置变量的测试用例先执行,后续用例即可正常访问。先排查常见问题,再看正确示例:
常见问题排查
- 确认测试用例执行顺序:Robot Framework默认按文件内顺序执行,若单独运行后续用例(如用
--test Testcase2指定),前置用例未执行则变量未定义。 - 检查
Set Suite Variable语法:必须明确指定变量名,避免语法错误。
正确示例(TC1_api.robot)
*** Settings *** Library RequestsLibrary *** Test Cases *** Testcase1: 创建资源并提取ID ${response}= Post Request https://your-api-url/create json={"name": "test"} ${created_id}= Get From Json ${response.json()} id Set Suite Variable ${created_id} # 将变量设为当前文件(套件)级变量 Testcase2: 使用已创建的ID Log 要使用的ID是: ${created_id} ${get_response}= Get Request https://your-api-url/detail/${created_id} Should Be Equal As Strings ${get_response.status_code} 200
二、跨测试用例文件夹的变量共享
跨文件夹属于不同测试套件,可通过以下三种方式实现变量共享:
方法1:使用全局变量
全局变量作用域覆盖整个Robot执行会话,所有测试文件均可访问。
示例:
- 在TC1_api.robot中设置全局变量:
Testcase1: 创建资源并设置全局ID ${response}= Post Request https://your-api-url/create json={"name": "test"} ${created_id}= Get From Json ${response.json()} id Set Global Variable ${created_id} # 标记为全局变量
- 在其他文件夹的测试文件(如
tests/subfolder/TC2_api.robot)中直接使用:
*** Test Cases *** Testcase3: 跨文件夹使用全局ID Log 跨文件夹获取的ID: ${created_id} ${get_response}= Get Request https://your-api-url/detail/${created_id} Should Be Equal As Strings ${get_response.status_code} 200
方法2:使用资源文件(推荐)
资源文件专门用于存放共享变量、关键字,便于统一维护。
步骤:
- 创建资源文件
resources/SharedResources.robot:
*** Variables *** ${SHARED_CREATED_ID} ${EMPTY} # 初始化空变量
- 在TC1_api.robot中导入资源文件并更新变量:
*** Settings *** Library RequestsLibrary Resource ../resources/SharedResources.robot # 根据实际路径导入 *** Test Cases *** Testcase1: 创建资源并更新共享变量 ${response}= Post Request https://your-api-url/create json={"name": "test"} ${created_id}= Get From Json ${response.json()} id Set Suite Variable ${SHARED_CREATED_ID} ${created_id} # 更新资源文件中的变量
- 在跨文件夹的测试文件中导入资源文件并使用:
*** Settings *** Library RequestsLibrary Resource ../../resources/SharedResources.robot # 根据实际路径导入 *** Test Cases *** Testcase3: 使用共享资源中的ID Log 从资源文件获取的ID: ${SHARED_CREATED_ID} ${get_response}= Get Request https://your-api-url/detail/${SHARED_CREATED_ID} Should Be Equal As Strings ${get_response.status_code} 200
方法3:使用Python变量文件(复杂场景适用)
适合需要动态生成或修改变量的场景,通过Python文件维护共享变量。
示例:
- 创建Python变量文件
shared_vars.py:
created_id = "" def set_created_id(value): global created_id created_id = value
- 在TC1_api.robot中导入并设置变量:
*** Settings *** Library RequestsLibrary Variables ../shared_vars.py *** Test Cases *** Testcase1: 创建资源并设置变量 ${response}= Post Request https://your-api-url/create json={"name": "test"} ${created_id}= Get From Json ${response.json()} id Call Method set_created_id ${created_id} # 调用Python函数更新变量
- 在跨文件夹的测试文件中导入并使用:
*** Settings *** Library RequestsLibrary Variables ../../shared_vars.py *** Test Cases *** Testcase3: 使用Python变量文件中的ID Log Python变量文件中的ID: ${created_id} ${get_response}= Get Request https://your-api-url/detail/${created_id} Should Be Equal As Strings ${get_response.status_code} 200
内容的提问来源于stack exchange,提问作者Abilash
相关产品推荐
相关产品推荐

