Snooz82 / robotframework-datadriver

Library to provide Data-Driven testing with CSV tables to Robot Framework
Apache License 2.0
131 stars 37 forks source link

Add possibility to configure DataDriver with a keyword (robot or python) #20

Closed aniketnjoshi closed 4 years ago

aniketnjoshi commented 4 years ago

We have a requirement where our data driver file gets generated at suite setup with random name. Is it possible to provide data driver file with random name in suite setup?

Snooz82 commented 4 years ago

The problem is, that the TestCases are generated before Suite Setup. So even if the file is modified at suite Setup it wouldn't have any impact, because generation is already done.

But: What do you think of the following idea?

I add an option do DataDriver to call a spacific Keyword.

Like Library DataDriver config_keyword=Config DataDriver

This Keyword can be defined by you globally or in each Suite individually.

I would design is so, that this Keyword must have a specific Interface, where it gets all configs from DataDriver and it must return a Dictionary where the content will be read as "new" config.

With this you could generade the CSV file, and define {'file': 'ga28rid6247hd.csv'} as a return value and DataDriver uses these information to create TestCases.

Snooz82 commented 4 years ago

I would call this Keyword with BuiltIn Features so it could be a robot keyword or a Python one.

aniketnjoshi commented 4 years ago

Thank you for your reply. When are you planning to add support for this request?

Snooz82 commented 4 years ago

First i would like to know if this would suite your needs.

aniketnjoshi commented 4 years ago

We are using xlsx file in data driver. As I understand, there would be way to provide keyword name which will be called where I can generate file and return file path in json format. Let me know if I understood it correctly. If this would be the way then it will satisfy my requirement.

Snooz82 commented 4 years ago

Hi,

This is the proof of concept.

The Keyword gets all arguments, that DataDriver gets from Library import, as Robot Dictionary. It has to return a dictionary with these keys you want to set/override.

Example

*** Settings ***
Library           OperatingSystem
Library           DataDriver    dialect=excel    encoding=utf_8   config_keyword=Config
Test Template     The Keyword

*** Test Cases ***
Test    aaa

*** Keywords ***
The Keyword
    [Arguments]    ${var}
    Log To Console    ${var}

Config
    [Arguments]    ${original_config}
    Log To Console    ${original_config.dialect}                #just a log of the original
    Create File    ${CURDIR}/test321.csv
    ...    *** Test Cases ***,\${var},\n123,111,\n321,222,      #generating file
    ${new_config}=    Create Dictionary    file=test321.csv     #set file attribute in a dictionary
    [Return]    ${new_config}                                   # returns {'file': 'test321.csv'}

Result:

==============================================================================
Test                                                                          
==============================================================================
excel
123                                                                   111
123                                                                   | PASS |
------------------------------------------------------------------------------
321                                                                   222
321                                                                   | PASS |
------------------------------------------------------------------------------
Test                                                                  | PASS |
2 critical tests, 2 passed, 0 failed
2 tests total, 2 passed, 0 failed
==============================================================================

I try to get time tomorrow and release this as beta, so that you can test it.

aniketnjoshi commented 4 years ago

Thank you

Snooz82 commented 4 years ago

You should be able to use it. pip install --upgrade --pre robotframework-datadriver[XLS]

the version 0.4.0b2 include an option config_keyword= This option enables you to name a keyword that will be called from DataDriver before it starts its work.

Example from previous post works.

The Config Keyword need to accept one argument, which will be the whole config with all values as robot framework dictionary. And the keyword must return a dictionary that shall include all config keys that should be changed.

Config
    [Arguments]    ${original_config}
    ${new_config}=    Create Dictionary    file=new.csv    encoding=cp1252
    [Return]    ${new_config} 
aniketnjoshi commented 4 years ago

Thank you @Snooz82 . This enhancement is working as expected. Could you please add this enhancement as a part of default installation.

lucagiove commented 4 years ago

Why not passing directly the configuration dictionary?

Snooz82 commented 4 years ago

@lucagiove

How and when would you generate it? The only solution would be a python variable file, that is imported before Robot Framework.

Snooz82 commented 4 years ago

But then it must be python. So you can also use a robot keyword.

aniketnjoshi commented 4 years ago

Hi @Snooz82 , When are you planning to make this GA?

Snooz82 commented 4 years ago

Hi @aniketnjoshi,

Maybe in the next two weeks. But you can use it. It will stay compatible to this behavior.

aniketnjoshi commented 4 years ago

Hi @Snooz82 ,

Could you please merge this enhancement?

Snooz82 commented 4 years ago

@aniketnjoshi Is available not with

pip install -U --pre robotframework-datadriver
Snooz82 commented 4 years ago

Will be fixed with 1.0 Release