Home > Mobile >  How to pass a dict to a unit test using parameterized?
How to pass a dict to a unit test using parameterized?

Time:09-12

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.

CodePudding user response:

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.

  • Related