Skip to content

Commit 2e7ecd7

Browse files
betapl3bdelatrie
andauthored
Add pytest ParameterSet.id placeholder for @allure.title formatter (#787)
Co-authored-by: Maxim <17935127+delatrie@users.noreply.github.com>
1 parent fe74f46 commit 2e7ecd7

File tree

4 files changed

+107
-3
lines changed

4 files changed

+107
-3
lines changed

allure-pytest/examples/display_name/display_name.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,8 @@ Titles support placeholders for arguments.
1717
... @pytest.mark.parametrize('param', [False])
1818
... def test_display_name_template(param):
1919
... assert param
20+
21+
>>> @allure.title("A test title with ParameterSet id {param_id}")
22+
... @pytest.mark.parametrize('param', [False], ids=["some_id"])
23+
... def test_display_name_parameter_set_id(param):
24+
... assert param

allure-pytest/src/listener.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,8 @@ def pytest_runtest_setup(self, item):
103103
uuid = self._cache.get(item.nodeid)
104104
test_result = self.allure_logger.get_test(uuid)
105105
params = self.__get_pytest_params(item)
106-
test_result.name = allure_name(item, params)
106+
param_id = self.__get_pytest_param_id(item)
107+
test_result.name = allure_name(item, params, param_id)
107108
full_name = allure_full_name(item)
108109
test_result.fullName = full_name
109110
test_result.testCaseId = md5(full_name)
@@ -307,6 +308,10 @@ def add_parameter(self, name, value, excluded, mode: ParameterMode):
307308
def __get_pytest_params(item):
308309
return item.callspec.params if hasattr(item, 'callspec') else {}
309310

311+
@staticmethod
312+
def __get_pytest_param_id(item):
313+
return item.callspec.id if hasattr(item, 'callspec') else None
314+
310315
def __apply_default_suites(self, item, test_result):
311316
default_suites = allure_suite_labels(item)
312317
existing_suites = {

allure-pytest/src/utils.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,12 +110,18 @@ def allure_package(item):
110110
return path.replace('/', '.')
111111

112112

113-
def allure_name(item, parameters):
113+
def allure_name(item, parameters, param_id=None):
114114
name = item.name
115115
title = allure_title(item)
116+
param_id_kwargs = {}
117+
if param_id:
118+
# if param_id is an ASCII string, it could have been encoded by pytest (_pytest.compat.ascii_escaped)
119+
if param_id.isascii():
120+
param_id = param_id.encode().decode("unicode-escape")
121+
param_id_kwargs["param_id"] = param_id
116122
return SafeFormatter().format(
117123
title,
118-
**{**parameters, **item.funcargs}
124+
**{**param_id_kwargs, **parameters, **item.funcargs}
119125
) if title else name
120126

121127

tests/allure_pytest/acceptance/display_name/display_name_test.py

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,3 +105,91 @@ def test_failed_fixture_value_in_display_name(allure_pytest_runner: AllurePytest
105105
has_title("title with {fix}")
106106
)
107107
)
108+
109+
110+
def test_param_id_in_display_name(allure_pytest_runner: AllurePytestRunner):
111+
"""
112+
>>> import allure
113+
>>> import pytest
114+
115+
>>> @pytest.mark.parametrize("name", [pytest.param("value", id="some id")])
116+
... @allure.title('Title with id - {param_id}')
117+
... def test_param_id(name):
118+
... pass
119+
"""
120+
121+
allure_results = allure_pytest_runner.run_docstring()
122+
123+
assert_that(
124+
allure_results,
125+
has_test_case(
126+
"test_param_id",
127+
has_title("Title with id - some id")
128+
)
129+
)
130+
131+
132+
def test_no_param_id_in_display_name(allure_pytest_runner: AllurePytestRunner):
133+
"""
134+
>>> import allure
135+
>>> import pytest
136+
137+
>>> @pytest.mark.parametrize("param1, param2", [pytest.param("value1", "value2")])
138+
... @allure.title('Title with id - {param_id}')
139+
... def test_no_param_id(param1, param2):
140+
... pass
141+
"""
142+
143+
allure_results = allure_pytest_runner.run_docstring()
144+
145+
assert_that(
146+
allure_results,
147+
has_test_case(
148+
"test_no_param_id",
149+
has_title("Title with id - value1-value2")
150+
)
151+
)
152+
153+
154+
def test_non_ascii_id_in_display_name(allure_pytest_runner: AllurePytestRunner):
155+
"""
156+
>>> import allure
157+
>>> import pytest
158+
159+
>>> @pytest.mark.parametrize("name", [pytest.param("value", id="Ид,本我,पहचान,بطاقة تعريف")])
160+
... @allure.title('Title with non-ASCII id - {param_id}')
161+
... def test_non_ascii_param_id(name):
162+
... pass
163+
"""
164+
165+
allure_results = allure_pytest_runner.run_docstring()
166+
167+
assert_that(
168+
allure_results,
169+
has_test_case(
170+
"test_non_ascii_param_id",
171+
has_title("Title with non-ASCII id - Ид,本我,पहचान,بطاقة تعريف")
172+
)
173+
)
174+
175+
176+
def test_explicit_parameter_called_param_id_in_display_name(allure_pytest_runner: AllurePytestRunner):
177+
"""
178+
>>> import allure
179+
>>> import pytest
180+
181+
>>> @pytest.mark.parametrize("param_id", [pytest.param("param value", id="some id")])
182+
... @allure.title('Title with id - {param_id}')
183+
... def test_explicit_parameter_called_param_id(param_id):
184+
... pass
185+
"""
186+
187+
allure_results = allure_pytest_runner.run_docstring()
188+
189+
assert_that(
190+
allure_results,
191+
has_test_case(
192+
"test_explicit_parameter_called_param_id",
193+
has_title("Title with id - param value")
194+
)
195+
)

0 commit comments

Comments
 (0)