Skip to content

Commit e82f180

Browse files
authored
index error in parametrzed tests (fixes #124 via #137)
1 parent 12a0382 commit e82f180

File tree

5 files changed

+20
-57
lines changed

5 files changed

+20
-57
lines changed

allure-pytest/src/listener.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from allure_commons.utils import now
55
from allure_commons.utils import md5
66
from allure_commons.utils import uuid4
7+
from allure_commons.utils import represent
78

89
from allure_commons.reporter import AllureReporter
910

@@ -15,7 +16,6 @@
1516
from allure_commons.model2 import Status
1617
from allure_commons.types import LabelType
1718

18-
from allure_pytest.utils import allure_parameters
1919
from allure_pytest.utils import allure_labels, allure_links, pytest_markers
2020
from allure_pytest.utils import allure_full_name, allure_package
2121

@@ -86,6 +86,9 @@ def pytest_runtest_protocol(self, item, nextitem):
8686
@pytest.hookimpl(hookwrapper=True)
8787
def pytest_runtest_call(self, item):
8888
uuid = self._cache.get(item.nodeid)
89+
for name, value in item.callspec.params.items() if hasattr(item, 'callspec') else ():
90+
self.allure_logger.update_test(uuid, parameters=Parameter(name, represent(value)))
91+
8992
self.allure_logger.update_test(uuid, start=now())
9093
yield
9194
self.allure_logger.update_test(uuid, stop=now())
@@ -107,12 +110,6 @@ def pytest_fixture_setup(self, fixturedef, request):
107110
before_fixture = TestBeforeResult(name=fixture_name, start=now())
108111
self.allure_logger.start_before_fixture(container_uuid, before_fixture_uuid, before_fixture)
109112

110-
parameters = allure_parameters(fixturedef, request)
111-
if parameters:
112-
test_uuid = self._cache.get(request._pyfuncitem.nodeid)
113-
parameters = Parameter(**parameters) if parameters else []
114-
self.allure_logger.update_test(test_uuid, parameters=parameters)
115-
116113
yield
117114

118115
self.allure_logger.stop_before_fixture(before_fixture_uuid, stop=now())

allure-pytest/src/utils.py

Lines changed: 0 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -2,52 +2,13 @@
22
from __future__ import unicode_literals
33

44
import os
5-
from itertools import product
65
from allure_commons.utils import represent
76

87
ALLURE_UNIQUE_LABELS = ['severity', 'thread', 'host']
98
ALLURE_LABEL_PREFIX = 'allure_label'
109
ALLURE_LINK_PREFIX = 'allure_link'
1110

1211

13-
def allure_parameters(fixturedef, request):
14-
parameters = {}
15-
param_name = request.fixturename
16-
17-
if hasattr(request, 'param'):
18-
parameters = {'name': fixturedef.ids[request.param_index] if fixturedef.ids else param_name,
19-
'value': str(request.param)}
20-
21-
if 'parametrize' in request.node.keywords.keys():
22-
param_map = list()
23-
for mark_info in request.node.keywords['parametrize']:
24-
25-
_ids = mark_info.kwargs['ids'] if 'ids' in mark_info.kwargs.keys() else None
26-
_args = mark_info.args[0]
27-
if not isinstance(_args, (tuple, list)):
28-
_args = [x.strip() for x in _args.split(",") if x.strip()]
29-
30-
param_map.append({'args': _args,
31-
'has_ids': _ids is not None,
32-
'ids': _ids if _ids else mark_info.args[1],
33-
'values': list()})
34-
35-
for variant in product(*[item['ids'] for item in param_map]):
36-
for i, item in enumerate(param_map):
37-
item['values'].append(variant[i])
38-
39-
for item in param_map:
40-
if param_name in item['args'] and item['has_ids']:
41-
ids = item['values'][request.param_index]
42-
if len(item['args']) == 1:
43-
parameters = {'name': ids, 'value': str(request.param)}
44-
else:
45-
param_name = '{ids}::{param}'.format(ids=ids, param=param_name)
46-
parameters = {'name': param_name, 'value': str(request.param)}
47-
48-
return parameters
49-
50-
5112
def allure_labels(item):
5213
for keyword in item.keywords.keys():
5314
if keyword.startswith(ALLURE_LABEL_PREFIX):

allure-pytest/test/fixtures/function_scope/fixtures_parametrization_test.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ def test_function_scope_parametrized_fixture_with_ids(parametrized_fixture_with_
3939
>>> for param, ids in zip([True, False], ['param_true', 'param_false']):
4040
... assert_that(allure_report,
4141
... has_test_case('test_function_scope_parametrized_fixture_with_ids[{ids}]'.format(ids=ids),
42-
... has_parameter(ids, str(param))
42+
... has_parameter('parametrized_fixture_with_ids', str(param))
4343
... )
4444
... )
4545
"""
@@ -55,7 +55,7 @@ def test_two_function_scope_parametrized_fixture(parametrized_fixture, parametri
5555
... has_test_case('test_two_function_scope_parametrized_fixture[{param1}-{ids2}]'.format(
5656
... param1=param1, ids2=ids2),
5757
... all_of(has_parameter('parametrized_fixture', str(param1)),
58-
... has_parameter(ids2, str(param2))
58+
... has_parameter('parametrized_fixture_with_ids', str(param2))
5959
... )
6060
... )
6161
... )

allure-pytest/test/parametrization/parametrization_deep_test.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ def test_parametrization_with_many_decorators_and_ids_for_first(param1, param2):
4646
... param2=param2,
4747
... num2=num2),
4848
... all_of(
49-
... has_parameter(ids1, str(param1)),
49+
... has_parameter('param1', str(param1)),
5050
... has_parameter('param2', str(param2))
5151
... )
5252
... ))
@@ -68,7 +68,7 @@ def test_parametrization_with_many_decorators_and_ids_for_second(param1, param2)
6868
... ids2=ids2),
6969
... all_of(
7070
... has_parameter('param1', str(param1)),
71-
... has_parameter(ids2, str(param2))
71+
... has_parameter('param2', str(param2))
7272
... )
7373
... ))
7474
"""
@@ -88,8 +88,8 @@ def test_parametrization_with_many_decorators_and_ids_for_all(param1, param2):
8888
... ids1=ids1,
8989
... ids2=ids2),
9090
... all_of(
91-
... has_parameter(ids1, str(param1)),
92-
... has_parameter(ids2, str(param2))
91+
... has_parameter('param1', str(param1)),
92+
... has_parameter('param2', str(param2))
9393
... )
9494
... ))
9595
"""
@@ -114,8 +114,8 @@ def test_parametrization_with_many_decorators_with_partial_ids_and_unsorted_args
114114
... ids3=ids3),
115115
... all_of(
116116
... has_parameter('param1', str(param1)),
117-
... has_parameter(ids2, str(param2)),
118-
... has_parameter(ids3, str(param3))
117+
... has_parameter('param2', str(param2)),
118+
... has_parameter('param3', str(param3))
119119
... )
120120
... ))
121121
"""

allure-pytest/test/parametrization/parametrization_simple_test.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@
1111
import pytest
1212

1313

14+
@pytest.mark.parametrize(argnames="value", argvalues=range(1))
15+
def test_with_parametrization_by_kwargs(value):
16+
return True
17+
18+
1419
@pytest.mark.parametrize('param', [True, False])
1520
def test_parametrization_one_param_without_ids(param):
1621
"""
@@ -32,7 +37,7 @@ def test_parametrization_one_param_with_ids(param):
3237
... ids = 'pass' if param else 'fail'
3338
... assert_that(allure_report,
3439
... has_test_case('test_parametrization_one_param_with_ids[{param}]'.format(param=ids),
35-
... has_parameter(ids, str(param))
40+
... has_parameter('param', str(param))
3641
... ))
3742
"""
3843
assert not param
@@ -79,8 +84,8 @@ def test_parametrization_many_params_with_ids(param1, param2):
7984
... assert_that(allure_report,
8085
... has_test_case('test_parametrization_many_params_with_ids[{ids}]'.format(ids=ids),
8186
... all_of(
82-
... has_parameter('{ids}::{param}'.format(ids=ids, param='param1'), str(param1)),
83-
... has_parameter('{ids}::{param}'.format(ids=ids, param='param2'), str(param2))
87+
... has_parameter('{param}'.format(ids=ids, param='param1'), str(param1)),
88+
... has_parameter('{param}'.format(ids=ids, param='param2'), str(param2))
8489
... )
8590
... ))
8691

0 commit comments

Comments
 (0)