Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Robot Framework get name of current test case

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?

like image 294
Lorkenpeist Avatar asked Mar 28 '14 17:03

Lorkenpeist


People also ask

Why is Robot Framework not good?

Cons of Using Robot Framework: Robot Framework is hard to maintain. Robot Framework HTML reports are difficult to customize. Robot Framework does not support parallel test execution. Some Robot Framework errors are difficult to debug.

How do you run test cases using tags in Robot Framework?

In case we want to execute all test suites in the project we will just replace Tests/Tags. robot with Tests. 'Tests' is the folder where we have all our Test Suites (or robot files). Another way to apply tags to the test cases is by using Force Tags in the Settings section.

What is force tags in Robot Framework?

The robot framework allows tags to give the test cases free metadata. The tags can be set in a file using “Force Tags” and “Default Tags”. It is possible to give tags for a single test case using [Tags] just like [Template].


2 Answers

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}
like image 54
Lorkenpeist Avatar answered Sep 23 '22 08:09

Lorkenpeist


Also, there is a ${SUITE NAME} variable that holds the main txt file's name.

like image 26
mdixey Avatar answered Sep 22 '22 08:09

mdixey