Skip to content
Advertisement

How to pass a dict to a unit test using parameterized?

I have a list of dicts:

MY_LIST = [ 
    { 'key1': {'a': 1, 'b':2 } },
    { 'key2': {'a': 1, 'b':2 } } 
]

How do I pass the dict to a django unit test using parameterized? E.g.

@parameterized.expand(MY_LIST):
def test_mytest(self, dict_item):
    print(dict_item.items())

Results in AttributeError: 'str' object has no attribute 'items' because the dict is being converted to a string.

Advertisement

Answer

As stated in the docs:

The @parameterized and @parameterized.expand decorators accept a list or iterable of tuples or param(...), or a callable which returns a list or iterable

So I would try to convert MY_LIST to:

MY_LIST = [ 
    ({ 'key1': {'a': 1, 'b': 2}},),
    ({ 'key2': {'a': 1, 'b': 2}},), 
]

Which makes it a list of tuples that contain a single parameter to apply to the method you are testing.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement