Snooz82/robotframework-datadriver

Variables imported with data-driver are not available to [Set Up] keywords

rook218 opened this issue · 7 comments

When datadriver creates variables, they are not available to the set up keyword. I am not sure if that's because the variables are scoped locally, or because the variables are not instantiated until after set up runs.

Many of our test needs require us to set up data in a DB in a specific format, and data-driver would be a perfect tool for this if we could pass datadriver variables to the set-up keyword.

As it stands now, we have to write every test manually and pass parameters to the [Set Up] keywords for every test and are not able to use robotframework-datadriver when [Set Up] is not completely identical in every case.

This is a typical use case that we'd like to run with a data sheet, but it is not possible.

*** Settings ***

Test Template    Get data from API

*** Test Cases ***
Get data from API for ${accountNumber} and date ${userDate}    Default    UserData

*** Keywords ***
Get data from API
    [Setup]     Setup Keyword       arg1=${accountNumber}
    [Arguments]     ${accountNumer}    ${userDate}
    ${response}=    Hit API endpoint    body={"accountNumber": "${accountNumber}", "date": ${userDate}}
    Should contain      ${response}     "valid"
    Should contain      ${response}     ${userDate}

Setup Keyword
    [Arguments]     ${arg1}=Default
    Log             ${arg1}
    Insert data to some DB      {"weNeedToCustomizeThisFieldForEachTestCase": "${arg1}"}

Hi,

You could just use that Setup Keyword directly as first call in that Get data from API Template.
You can not use [Setup] in keywords and even in Test Cases afaik the only "feature" of [Setup] is, that it is a little bit different in the log.html...

*** Settings ***
Library     DataDriver
Test Template    Get data from API

*** Test Cases ***
Get data from API for ${accountNumber} and date ${userDate}    Default    UserData

*** Keywords ***
Get data from API
    [Arguments]     ${accountNumer}    ${userDate}
    Setup Keyword       arg1=${accountNumber}
    ${response}=    Hit API endpoint    body={"accountNumber": "${accountNumber}", "date": ${userDate}}
    Should contain      ${response}     "valid"
    Should contain      ${response}     ${userDate}

Setup Keyword
    [Arguments]     ${arg1}=Default
    Log             ${arg1}
    Insert data to some DB      {"weNeedToCustomizeThisFieldForEachTestCase": "${arg1}"}

As far as i know, it is also without DataDriver not possible to manipulate the Test Setup by the Data-Driven approach.

Knowing what you know about the Robot API, would it be possible to feed variables into the [Setup] keyword? On top of being more explicit when set up failed in our reports, I like to keep everything syntactic and "to standard." Other than that, I think you're right that there wouldn't be any functional difference.

Puh that is a hard one.

maybe it would be possible to set a Robot Test Case Variable each time a start_test happens.
But that would also have other side effects.

have to check this. But no time.

if you want to try it by yourself and report what happened.
Use a listener version 3 and try to use BuiltIn().set_test_variable(...) in the start_test(...) method of the listener.

if this works, it maybe would be worth the effort

Thanks for the head start, I will take a look over the next couple of days

I have found that the method you suggested works to set a suite variable which the test set up will have access to. But I don't believe there is a way to get access to the variables of the test case from the listener. Per the docs, we'd have access to all attributes of the TestCase model in the Running submodule, and variables isn't an attribute of the current test case.

https://robot-framework.readthedocs.io/en/master/autodoc/robot.running.html#robot.running.model.TestCase