Skip to content

Commit

Permalink
Add pytest ParameterSet.id placeholder for @allure.title formatter
Browse files Browse the repository at this point in the history
  • Loading branch information
betapl3b committed Jan 2, 2024
1 parent f5db4f4 commit 92c9434
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 3 deletions.
7 changes: 6 additions & 1 deletion allure-pytest/src/listener.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,8 @@ def pytest_runtest_setup(self, item):
uuid = self._cache.get(item.nodeid)
test_result = self.allure_logger.get_test(uuid)
params = self.__get_pytest_params(item)
test_result.name = allure_name(item, params)
param_id = self.__get_pytest_param_id(item)
test_result.name = allure_name(item, params, param_id)
full_name = allure_full_name(item)
test_result.fullName = full_name
test_result.testCaseId = md5(full_name)
Expand Down Expand Up @@ -307,6 +308,10 @@ def add_parameter(self, name, value, excluded, mode: ParameterMode):
def __get_pytest_params(item):
return item.callspec.params if hasattr(item, 'callspec') else {}

@staticmethod
def __get_pytest_param_id(item):
return item.callspec.id if hasattr(item, 'callspec') else None

def __apply_default_suites(self, item, test_result):
default_suites = allure_suite_labels(item)
existing_suites = {
Expand Down
10 changes: 8 additions & 2 deletions allure-pytest/src/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,12 +110,18 @@ def allure_package(item):
return path.replace('/', '.')


def allure_name(item, parameters):
def allure_name(item, parameters, param_id=None):
name = item.name
title = allure_title(item)
param_id_kwargs = {}
if param_id:
# if param_id is an ASCII string, it could have been encoded by pytest (_pytest.compat.ascii_escaped)
if param_id.isascii():
param_id = param_id.encode().decode("unicode-escape")
param_id_kwargs["param_id"] = param_id
return SafeFormatter().format(
title,
**{**parameters, **item.funcargs}
**{**parameters, **item.funcargs, **param_id_kwargs}
) if title else name


Expand Down
64 changes: 64 additions & 0 deletions tests/allure_pytest/acceptance/display_name/display_name_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,3 +105,67 @@ def test_failed_fixture_value_in_display_name(allure_pytest_runner: AllurePytest
has_title("title with {fix}")
)
)


def test_param_id_in_display_name(allure_pytest_runner: AllurePytestRunner):
"""
>>> import allure
>>> import pytest
>>> @pytest.mark.parametrize("name", [pytest.param("value", id="some id")])
... @allure.title('Title with id - {param_id}')
... def test_param_id(name):
... pass
"""

allure_results = allure_pytest_runner.run_docstring()

assert_that(
allure_results,
has_test_case(
"test_param_id",
has_title("Title with id - some id")
)
)

def test_no_param_id_in_display_name(allure_pytest_runner: AllurePytestRunner):
"""
>>> import allure
>>> import pytest
>>> @pytest.mark.parametrize("param1, param2", [pytest.param("value1", "value2")])
... @allure.title('Title with id - {param_id}')
... def test_no_param_id(param1, param2):
... pass
"""

allure_results = allure_pytest_runner.run_docstring()

assert_that(
allure_results,
has_test_case(
"test_no_param_id",
has_title("Title with id - value1-value2")
)
)

def test_non_ascii_id_in_display_name(allure_pytest_runner: AllurePytestRunner):
"""
>>> import allure
>>> import pytest
>>> @pytest.mark.parametrize("name", [pytest.param("value", id="Ид,本我,पहचान,بطاقة تعريف")])
... @allure.title('Title with non-ASCII id - {param_id}')
... def test_non_ascii_param_id(name):
... pass
"""

allure_results = allure_pytest_runner.run_docstring()

assert_that(
allure_results,
has_test_case(
"test_non_ascii_param_id",
has_title("Title with non-ASCII id - Ид,本我,पहचान,بطاقة تعريف")
)
)

0 comments on commit 92c9434

Please sign in to comment.