Reputation: 1495
I have a set of test data that is categorized by test case, and I would like to have a test setup keyword that loads the data for each test case. Something like this would work:
*** Keywords ***
Load Test Data
[Arguments] ${test case name}
${data}= Get File ${test case name}.txt
Set Test Variable ${data} ${data}
*** Testcases ***
First Test
Load Test Data First Test
Log ${data}
Second Test
Load Test Data Second Test
Log ${data}
However, it would be nice not to have to include the "Load Test Data" keyword at the beginning of each test case. Is there a keyword that can get the name of the test case, so I can call it inside of "Load Test Case" and make it a test setup keyword, like so?
*** Settings ***
Test Setup Load Test Data
*** Keywords ***
Load Test Data
${test case name}= Get Test Case Name
${data}= Get File ${test case name}.txt
Set Test Variable ${data} ${data}
*** Testcases ***
First Test
Log ${data}
Second Test
Log ${data}
Basically, what would the equivalent of "Get Test Case Name" be?
If it can't be done easily using Robot Framework keywords, I don't mind getting my hands dirty with Python. I could create "Load Test Data" as a Python library if necessary. Is there perhaps a class that stores the name of the current test case, that I could access?
Upvotes: 20
Views: 34374
Reputation: 1495
After a bit of digging, I was able to find this in the documentation:
There is a built-in variable ${TEST NAME}
, so my test case would look like:
*** Settings ***
Test Setup Load Test Data
*** Keywords ***
Load Test Data
${data}= Get File ${TEST NAME}.txt
Set Test Variable ${data} ${data}
*** Testcases ***
First Test
Log ${data}
Second Test
Log ${data}
Upvotes: 48
Reputation: 469
Just as additional comment and idea, if you want to handle files and more complex logic, have more control, it's perhaps good idea to write your custom robot library in Python.
When doing that, you can access also the "automatic variables" or built-in robot vars this way:
from robot.libraries.BuiltIn import BuiltIn
BuiltIn().get_variable_value("${TEST NAME}")
Upvotes: 7
Reputation: 133
Key word "Get Title" returns current test case file name the following code works for me:
Load Test Data
${title}= get title
Set Test Variable ${data} ${title}
Log ${data}
Upvotes: 0
Reputation: 21
This feature is useful when an error occurs in any test case, and you want to rename the screenshot file with the failed test name, by using Teardown.
Test Teardown
Take Screenshot ${TEST NAME}.jpeg
Kill Program
Make sure that ${TEST NAME} has no empty spaces and no \ / characters in it, otherwise RIDE will output an error, assuming a directory instead of a filename. I was struggling a lot to find this one.
Upvotes: 2
Reputation: 766
You can use ${TEST_NAME}
if you want the name of test case inside a suite,${SUITE_NAME}
to get the name of the file which contains the test cases ( Generally referred as Test Suite).
Upvotes: 3
Reputation: 81
Also, there is a ${SUITE NAME}
variable that holds the main txt file's name.
Upvotes: 7