|
| 1 | +from distutils.version import LooseVersion |
| 2 | + |
1 | 3 | import pytest
|
2 | 4 | from pytest_cases import param_fixture, param_fixtures, pytest_fixture_plus
|
3 | 5 |
|
| 6 | + |
| 7 | +# pytest.param - not available in all versions |
| 8 | +if LooseVersion(pytest.__version__) >= LooseVersion('3.0.0'): |
| 9 | + pytest_param = pytest.param |
| 10 | +else: |
| 11 | + def pytest_param(*args, **kwargs): |
| 12 | + return args |
| 13 | + |
| 14 | + |
4 | 15 | # create a single parameter fixture
|
5 | 16 | my_parameter = param_fixture("my_parameter", [1, 2, 3, 4])
|
6 | 17 |
|
@@ -39,42 +50,59 @@ def test_uses_param2(arg1, arg2, fixture_uses_param2):
|
39 | 50 |
|
40 | 51 | @pytest_fixture_plus
|
41 | 52 | @pytest.mark.parametrize("arg1, arg2", [
|
42 |
| - pytest.param(1, 2, id="f_a"), |
43 |
| - pytest.param(3, 4, id="f_b") |
| 53 | + pytest_param(1, 2, id="f_a"), |
| 54 | + pytest_param(3, 4, id="f_b") |
44 | 55 | ])
|
45 | 56 | def myfix(arg1, arg2, parg1):
|
46 | 57 | """One parameterized fixture relying on above param fixture"""
|
47 | 58 | return arg1, arg2, parg1
|
48 | 59 |
|
49 | 60 |
|
50 | 61 | @pytest.mark.parametrize("arg3, arg4", [
|
51 |
| - pytest.param(10, 20, id="t_a"), |
52 |
| - pytest.param(30, 40, id="t_b") |
| 62 | + pytest_param(10, 20, id="t_a"), |
| 63 | + pytest_param(30, 40, id="t_b") |
53 | 64 | ])
|
54 | 65 | def test_one(myfix, arg3, arg4, parg1, parg2, request):
|
55 | 66 | """"""
|
56 | 67 | assert myfix[2] == parg1
|
57 | 68 | paramvalues = request.node.nodeid.split('[')[1][:-1]
|
58 |
| - arg1arg2id = "f_a" if myfix[:-1] == (1, 2) else "f_b" |
59 |
| - arg3arg4id = "t_a" if (arg3, arg4) == (10, 20) else "t_b" |
| 69 | + if LooseVersion(pytest.__version__) >= LooseVersion('3.0.0'): |
| 70 | + arg1arg2id = "f_a" if myfix[:-1] == (1, 2) else "f_b" |
| 71 | + arg3arg4id = "t_a" if (arg3, arg4) == (10, 20) else "t_b" |
| 72 | + else: |
| 73 | + arg1arg2id = "-".join(["%s" % v for v in myfix[:-1]]) |
| 74 | + arg3arg4id = "-".join(["%s" % v for v in (arg3, arg4)]) |
| 75 | + |
60 | 76 | assert paramvalues == "{}-{}-{}-{}".format(arg1arg2id, parg1, parg2, arg3arg4id)
|
61 | 77 | # print("parg1={} parg2={} myfix={} arg3={} arg4={}".format(parg1, parg2, myfix, arg3, arg4))
|
62 | 78 |
|
63 | 79 |
|
64 | 80 | def test_synthesis(module_results_dct):
|
65 | 81 | """Use pytest-harvest to check that the list of executed tests is correct """
|
66 | 82 |
|
| 83 | + if LooseVersion(pytest.__version__) >= LooseVersion('3.0.0'): |
| 84 | + end_list = ['test_one[f_a-a-b-t_a]', |
| 85 | + 'test_one[f_a-a-b-t_b]', |
| 86 | + 'test_one[f_a-c-d-t_a]', |
| 87 | + 'test_one[f_a-c-d-t_b]', |
| 88 | + 'test_one[f_b-a-b-t_a]', |
| 89 | + 'test_one[f_b-a-b-t_b]', |
| 90 | + 'test_one[f_b-c-d-t_a]', |
| 91 | + 'test_one[f_b-c-d-t_b]'] |
| 92 | + else: |
| 93 | + end_list = [ 'test_one[1-2-a-b-10-20]', |
| 94 | + 'test_one[1-2-a-b-30-40]', |
| 95 | + 'test_one[1-2-c-d-10-20]', |
| 96 | + 'test_one[1-2-c-d-30-40]', |
| 97 | + 'test_one[3-4-a-b-10-20]', |
| 98 | + 'test_one[3-4-a-b-30-40]', |
| 99 | + 'test_one[3-4-c-d-10-20]', |
| 100 | + 'test_one[3-4-c-d-30-40]'] |
| 101 | + |
67 | 102 | assert list(module_results_dct) == ['test_uses_param[1]',
|
68 | 103 | 'test_uses_param[2]',
|
69 | 104 | 'test_uses_param[3]',
|
70 | 105 | 'test_uses_param[4]',
|
71 | 106 | 'test_uses_param2[1-2]',
|
72 | 107 | 'test_uses_param2[3-4]',
|
73 |
| - 'test_one[f_a-a-b-t_a]', |
74 |
| - 'test_one[f_a-a-b-t_b]', |
75 |
| - 'test_one[f_a-c-d-t_a]', |
76 |
| - 'test_one[f_a-c-d-t_b]', |
77 |
| - 'test_one[f_b-a-b-t_a]', |
78 |
| - 'test_one[f_b-a-b-t_b]', |
79 |
| - 'test_one[f_b-c-d-t_a]', |
80 |
| - 'test_one[f_b-c-d-t_b]'] |
| 108 | + ] + end_list |
0 commit comments