Skip to content

Instantly share code, notes, and snippets.

@poros
Created October 6, 2015 01:38
Show Gist options
  • Save poros/4ce8cedd0c2311b3d7fd to your computer and use it in GitHub Desktop.
Save poros/4ce8cedd0c2311b3d7fd to your computer and use it in GitHub Desktop.
Parametrized pytest testcase with dictionary of parameters and human readable testcase names
test_params = {
'empty_line': ('', {}),
'get_ok': ('GET 200', {'request': 'GET', 'status': '200'}),
'get_not_found': ('GET 404', {'request': 'GET', 'status': '404'}),
}
@pytest.mark.parametrize('line,expected', test_params.values(), ids=test_params.keys())
def test_decode(self, line, expected):
assert Decoder().decode(line) == expected
@lassebenni
Copy link

lassebenni commented Jul 13, 2018

Thank you!

In case someone copy&pastes this solution in Python 3.x, you have to wrap test_params.keys() with list because

In Python 3, dict.keys doesn't return a list, but a set-like object that represents a view of the dictionary's keys and (being set-like) doesn't
support indexing.

To fix the problem, use list(somedict.keys()) to collect the keys, and work with that.

ids=list(test_params.keys())

source: https://stackoverflow.com/questions/17322668/typeerror-dict-keys-object-does-not-support-indexing

@preciouswater
Copy link

Thank you!!
That was very helpful! saved me considerable amount of time.!

@cgebbe
Copy link

cgebbe commented Feb 12, 2021

Thank you! Very helpful and concise :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment