You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Robot Framework DataDriver库时提示${username}变量未找到求助

Fixing the "Variable '${username}' not found" Error with DataDriver in Robot Framework

Hey there! Let's get your DataDriver script working properly. The error you're seeing comes from a small formatting misstep in your CSV file—let's break it down and fix it step by step.

What's Causing the Issue?

Your CSV header uses ${username} and ${password} with Robot's variable syntax (${}), but DataDriver doesn't need that. The library treats header names directly as variable names, so when you use ${username} as a column name, it tries to create a variable named ${${username}} (instead of just ${username}), which doesn't match what your keyword expects. Also, the *** Test Cases *** header for the first column is unnecessary—DataDriver automatically recognizes the first column as test case names.

Fixed CSV File

Update your CSV to use clean variable names and a proper header structure:

Test Case Name,username,password,[Tags],[Documentation]
Valid user,user@mail.com,pass123,Positive,This is valid user credential
Invalid user,invalid@mail.com,pass123,Negative,This user is invalid
Invalid password,user@mail.com,pass,Negative,This password is invalid
Blank user,${EMPTY},pass123,Negative,Blank user
Blank password,user@mail.com,${EMPTY},Negative,Blank password
Blank user and password,${EMPTY},${EMPTY},Negative,Blank both user and password
  • Test Case Name: Labels the first column clearly (optional but makes the CSV easier to read)
  • username / password: Plain variable names (no ${}) that exactly match the arguments in your Test Credential keyword
  • [Tags] / [Documentation]: These are special DataDriver columns that automatically apply tags and documentation to each generated test case

Your Robot Script Stays Mostly the Same

You don't need to modify your .robot file—your existing setup is correct:

*** Settings ***
Library    DataDriver    file=resources/user_password.csv    encoding=utf_8    dialect=unix
Test Template    Test Credential

*** Test Cases ***
Doing Test Credentials for ${username} and ${password}

*** Keywords ***
Test Credential
    [Arguments]    ${username}    ${password}
    Log    ${username}
    Log    ${password}

That single test case line is all you need—DataDriver will generate individual test cases for every row in your CSV automatically.

Why This Works

When you run the script now:

  1. DataDriver reads the CSV and creates ${username} and ${password} variables for each row
  2. It uses your Test Credential keyword as a template to execute each test case
  3. The ${EMPTY} values will be correctly recognized as Robot's empty variable

Give this a try, and your "Variable not found" error should disappear!

内容的提问来源于stack exchange,提问作者Welly

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.09 20:38:12