Skip to content

Commit

Permalink
Add pytest ParameterSet.id placeholder for @allure.title formatter (a…
Browse files Browse the repository at this point in the history
  • Loading branch information
betapl3b and delatrie authored Feb 16, 2024
1 parent c9d93ae commit 3ae1278
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 3 deletions.
5 changes: 5 additions & 0 deletions allure-pytest/examples/display_name/display_name.rst
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,8 @@ Titles support placeholders for arguments.
... @pytest.mark.parametrize('param', [False])
... def test_display_name_template(param):
... assert param

>>> @allure.title("A test title with ParameterSet id {param_id}")
... @pytest.mark.parametrize('param', [False], ids=["some_id"])
... def test_display_name_parameter_set_id(param):
... assert param
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}
**{**param_id_kwargs, **parameters, **item.funcargs}
) if title else name


Expand Down
88 changes: 88 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,91 @@ 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 - Ид,本我,पहचान,بطاقة تعريف")
)
)


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

allure_results = allure_pytest_runner.run_docstring()

assert_that(
allure_results,
has_test_case(
"test_explicit_parameter_called_param_id",
has_title("Title with id - param value")
)
)

0 comments on commit 3ae1278

Please sign in to comment.