-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtesting.py
57 lines (46 loc) · 1.66 KB
/
testing.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
"""Utilities for testing."""
from typing import Any, Dict
import pytest
def subtests(subtest_dictionary: Dict[str, Dict[str, Any]]):
"""Wrapped pytest.mark.parametrize decorator (to make it more readable).
For example, instead of:
@pytest.mark.parametrize(
'test_input,expected',
[('3+5', 8), ('6*9', 42)],
ids=['addition', 'multiplication'])
def test_eval(test_input, expected):
assert eval(test_input) == expected
this allows to write:
@subtests({
'addition': dict(
test_input='3+5',
expected=8
),
'multiplication': dict(
test_input='6*9',
expected=42
)
})
def test_eval(test_input, expected):
assert eval(test_input) == expected
which is more readable, especially when defining many subtests.
Args:
subtest_dictionary: Dictionary of dictionaries, where the keys of the outer
dictionary are the subtest ids, and the inner dictionary contains subtest
parameters stored as key: value pairs of the form argname: argvalue. Since
pytest.mark.parametrize works with positional arguments only, all subtests
must specify the same set of arguments, or the function will throw a
KeyError.
Returns:
The equivalent pytest.mark.parametrize fixture (see example above).
"""
argnames = set()
for v in subtest_dictionary.values():
for k in v.keys():
argnames.add(k)
argnames = sorted(argnames)
return pytest.mark.parametrize(
argnames=argnames,
argvalues=[[v[k] for k in argnames] for v in subtest_dictionary.values()],
ids=subtest_dictionary.keys(),
)