使用Robot Framework DataDriver库时提示${username}变量未找到求助
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 yourTest Credentialkeyword[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:
- DataDriver reads the CSV and creates
${username}and${password}variables for each row - It uses your
Test Credentialkeyword as a template to execute each test case - 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

