Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to parse dicts of dicts in JSON to pass kwargs. #22

Merged
merged 4 commits into from
Sep 23, 2015

Conversation

garymacindoe
Copy link
Contributor

I have test functions with a large number of arguments:

def test_function(arg0, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9):
    pass

Using the current version of ddt I can write my test cases as a dict of lists in JSON and get reasonably predictable automatically generated test names:

{
    "test_case1": [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ],
    "test_case2": [ 0, 9, 8, 7, 6, 5, 4, 3, 2, 1 ]
}

Due to the large number of arguments involved it rapidly becomes confusing mapping each argument value in the list to each named argument in the test function and it also makes refactoring the test function (specifically reordering the arguments) near impossible.

I have added a few lines to ddt which check whether the test arguments are a dict and passes them to the test function as kwargs rather than assuming they will always be a list and passing them as args. This allows me to specify my test cases as a dict of dicts in JSON with named arguments while retaining predictable test case names and allowing the reordering of arguments in the test function:

{
    "test_case1": { "arg0": 0,
                            "arg1": 1,
                            "arg2": 2,
                            "arg3": 3,
                            "arg4": 4,
                            "arg5": 5,
                            "arg6": 6,
                            "arg7": 7,
                            "arg8": 8,
                            "arg9": 9 },
    "test_case2": { "arg0": 0,
                            "arg1": 9,
                            "arg2": 8,
                            "arg3": 7,
                            "arg4": 6,
                            "arg5": 5,
                            "arg6": 4,
                            "arg7": 3,
                            "arg8": 2,
                            "arg9": 1 }
}

@txels
Copy link
Collaborator

txels commented Jan 23, 2015

Hey sorry this had fallen off my radar completely. Apologies for that. I will look at it shortly.

if isinstance(value, dict):
add_test(test_name, func, **value)
else:
add_test(test_name, func, value)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks simple and useful, although it may break backwards compatibility.

Can you illustrate with an example/test case in test_example.py?

@txels
Copy link
Collaborator

txels commented Jan 23, 2015

Thanks for your contribution, and apologies again for the very late reply.

@garymacindoe
Copy link
Contributor Author

I've pulled in the changes from your master branch and updated my modifications to fit in with your refactored code. Thanks for taking the time to review the changes.

I've also added a test case using a dict-of-dicts specified in a JSON file to specify kwargs to a test function. The rest of the test cases continue to pass.

@keradus
Copy link

keradus commented Sep 22, 2015

Any news?
It would be really great to have that feature!

txels added a commit that referenced this pull request Sep 23, 2015
Add ability to parse dicts of dicts in JSON to pass kwargs.
@txels txels merged commit 7ed3a52 into datadriventests:master Sep 23, 2015
@txels
Copy link
Collaborator

txels commented Sep 23, 2015

Thanks.
Aplogies again for losing awareness of this.

@keradus
Copy link

keradus commented Sep 23, 2015

Great, thanks !

@clintonb
Copy link

Super-late to the party. I have an older library using version 1.0.0 that I want to update. This change broke the contract for @file_data defined in http://ddt.readthedocs.io/en/latest/example.html. The documentation should be updated to reflect the fact that dictionaries are expanded instead of passed as a single value (e.g. **kwargs instead of kwargs).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants