Skip to content

Commit

Permalink
Change the default name function of parameters testcase.
Browse files Browse the repository at this point in the history
  • Loading branch information
yuxuan-ms committed Dec 4, 2024
1 parent c3e5747 commit 6a0cddb
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 38 deletions.
1 change: 1 addition & 0 deletions doc/newsfragments/3160_changed.case_name.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Limit the length of parameterization testcase name to 255 characters. If the name length exceeds 255 characters, index-suffixed names (e.g., {func_name[:240]} 1, {func_name[:240]} 2) will be used.
41 changes: 34 additions & 7 deletions testplan/testing/multitest/parametrization.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from testplan.common.utils import callable as callable_utils
from testplan.common.utils import convert, interface
from testplan.testing import tagging
from typing import Callable, Optional

# Although any string will be processed as normal, it's a good
# approach to warn the user if the generated method name is not a
Expand Down Expand Up @@ -206,8 +207,11 @@ def _generated(self, env, result):
)
# Users request the feature that when `name_func` set to `None`,
# then simply append integer suffixes to the names of testcases
_generated.name = (
name_func(name, kwargs) if name_func is not None else f"{name} {idx}"
_generated.name = _parametrization_report_name_func_wrapper(
name_func=name_func,
name=name,
kwargs=kwargs,
index=idx,
)

if hasattr(function, "__xfail__"):
Expand Down Expand Up @@ -267,7 +271,7 @@ def _check_tag_func(tag_func):
)


def _parametrization_name_func_wrapper(func_name, kwargs):
def _parametrization_name_func_wrapper(func_name: str, kwargs: dict):
"""
Make sure that name generation doesn't end up with invalid / unreadable
attribute names/types etc.
Expand All @@ -291,6 +295,31 @@ def _parametrization_name_func_wrapper(func_name, kwargs):
return generated_name


def _parametrization_report_name_func_wrapper(
name_func: Optional[Callable], name: str, kwargs: dict, index: int
):
"""
Make sure that generated name is not too long,
if it is, then use index suffixed names e.g. "{func_name} 1", "{func_name} 2", will be used.
"""
if name_func:
generated_name = name_func(name, kwargs)
if not isinstance(generated_name, str):
raise ValueError(
"The return value of name_func must be a string, "
f"it is of type: {type(generated_name)}, value: {generated_name}"
)
if len(generated_name) <= MAX_METHOD_NAME_LENGTH:
return generated_name
else:
warnings.warn(
f"The name name_func returned ({generated_name}) is too long, using index suffixed names."
)

generated_name = f"{name[:MAX_METHOD_NAME_LENGTH - 10]} {index}"
return generated_name


def parametrization_name_func(func_name, kwargs):
"""
Method name generator for parametrized testcases.
Expand Down Expand Up @@ -321,7 +350,7 @@ def default_name_func(func_name, kwargs):
>>> import collections
>>> default_name_func('Test Method',
collections.OrderedDict(('foo', 5), ('bar', 10)))
'Test Method {foo:5, bar:10}'
'Test Method <foo:5, bar:10>'
:param func_name: Name of the parametrization target function.
:type func_name: ``str``
Expand All @@ -334,9 +363,7 @@ def default_name_func(func_name, kwargs):
"""
arg_strings = ["{arg}={{{arg}}}".format(arg=arg) for arg in kwargs]
template = "{func_name} <" + ", ".join(arg_strings) + ">"
return template.format(
func_name=func_name, **{k: repr(v) for k, v in kwargs.items()}
)
return template.format(func_name=func_name, **kwargs)


def generate_functions(
Expand Down
4 changes: 2 additions & 2 deletions testplan/testing/multitest/suite.py
Original file line number Diff line number Diff line change
Expand Up @@ -581,9 +581,9 @@ def _validate_testcase(func):

if len(func.name) > defaults.MAX_TEST_NAME_LENGTH:
warnings.warn(
'Name defined for testcase "{}" is too long,'
f'Name defined for testcase "{func.name}" is too long,'
' consider customizing testcase name with argument "name_func"'
" in @testcase decorator.".format(func.__name__)
" in @testcase decorator."
)


Expand Down
42 changes: 21 additions & 21 deletions tests/functional/testplan/testing/multitest/test_parametrization.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,19 +143,19 @@ def test_sample(self, env, result, a, b):
category=ReportCategories.PARAMETRIZATION,
entries=[
TestCaseReport(
name="test_sample <a=1, b='alpha'>",
name="test_sample <a=1, b=alpha>",
entries=[{"type": "IsTrue", "description": "1 - alpha"}],
),
TestCaseReport(
name="test_sample <a=1, b='beta'>",
name="test_sample <a=1, b=beta>",
entries=[{"type": "IsTrue", "description": "1 - beta"}],
),
TestCaseReport(
name="test_sample <a=2, b='alpha'>",
name="test_sample <a=2, b=alpha>",
entries=[{"type": "IsTrue", "description": "2 - alpha"}],
),
TestCaseReport(
name="test_sample <a=2, b='beta'>",
name="test_sample <a=2, b=beta>",
entries=[{"type": "IsTrue", "description": "2 - beta"}],
),
],
Expand Down Expand Up @@ -297,16 +297,16 @@ def sample(self, env, result, test__val):
(
(
("#@)$*@#%", "a-b"),
["sample_test <val='#@)$*@#%'>", "sample_test <val='a-b'>"],
["sample_test <val=#@)$*@#%>", "sample_test <val=a-b>"],
["sample_test__0", "sample_test__1"],
"Should use original method name + index fallback if"
" generated names are not valid Python attribute names.",
),
(
("a" * MAX_METHOD_NAME_LENGTH, "b" * MAX_METHOD_NAME_LENGTH),
[
"sample_test <val='{}'>".format("a" * MAX_METHOD_NAME_LENGTH),
"sample_test <val='{}'>".format("b" * MAX_METHOD_NAME_LENGTH),
"sample_test 0",
"sample_test 1",
],
["sample_test__0", "sample_test__1"],
"Should use original method name + index fallback if"
Expand Down Expand Up @@ -363,8 +363,8 @@ def sample_test(self, env, result, a, b):
name="Sample Test",
category=ReportCategories.PARAMETRIZATION,
entries=[
TestCaseReport(name="Sample Test <a='foo', b='bar'>"),
TestCaseReport(name="Sample Test <a='alpha', b='beta'>"),
TestCaseReport(name="Sample Test <a=foo, b=bar>"),
TestCaseReport(name="Sample Test <a=alpha, b=beta>"),
],
)

Expand Down Expand Up @@ -599,13 +599,13 @@ def dummy_test(self, env, result, color):
tags={"simple": {"alpha"}},
entries=[
TestCaseReport(
name="dummy_test <color='red'>", tags={"color": {"red"}}
name="dummy_test <color=red>", tags={"color": {"red"}}
),
TestCaseReport(
name="dummy_test <color='blue'>", tags={"color": {"blue"}}
name="dummy_test <color=blue>", tags={"color": {"blue"}}
),
TestCaseReport(
name="dummy_test <color='green'>", tags={"color": {"green"}}
name="dummy_test <color=green>", tags={"color": {"green"}}
),
],
)
Expand Down Expand Up @@ -652,29 +652,29 @@ def dummy_test_5(self, env, result, smell):
name="dummy_test_1",
category=ReportCategories.PARAMETRIZATION,
entries=[
TestCaseReport(name="dummy_test_1 <color='red'>"),
TestCaseReport(name="dummy_test_1 <color='blue'>"),
TestCaseReport(name="dummy_test_1 <color='green'>"),
TestCaseReport(name="dummy_test_1 <color=red>"),
TestCaseReport(name="dummy_test_1 <color=blue>"),
TestCaseReport(name="dummy_test_1 <color=green>"),
],
),
TestCaseReport(name="dummy_test_2"),
TestGroupReport(
name="dummy_test_3",
category=ReportCategories.PARAMETRIZATION,
entries=[
TestCaseReport(name="dummy_test_3 <shape='circle'>"),
TestCaseReport(name="dummy_test_3 <shape='square'>"),
TestCaseReport(name="dummy_test_3 <shape='triangle'>"),
TestCaseReport(name="dummy_test_3 <shape=circle>"),
TestCaseReport(name="dummy_test_3 <shape=square>"),
TestCaseReport(name="dummy_test_3 <shape=triangle>"),
],
),
TestCaseReport(name="dummy_test_4"),
TestGroupReport(
name="dummy_test_5",
category=ReportCategories.PARAMETRIZATION,
entries=[
TestCaseReport(name="dummy_test_5 <smell='fragrant'>"),
TestCaseReport(name="dummy_test_5 <smell='stinky'>"),
TestCaseReport(name="dummy_test_5 <smell='musty'>"),
TestCaseReport(name="dummy_test_5 <smell=fragrant>"),
TestCaseReport(name="dummy_test_5 <smell=stinky>"),
TestCaseReport(name="dummy_test_5 <smell=musty>"),
],
),
]
Expand Down
16 changes: 8 additions & 8 deletions tests/functional/testplan/testing/test_tagging.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,11 @@ def test_param_2(self, env, result, value):
tags={"speed": {"slow"}},
entries=[
TestCaseReport(
name="test_param <value='AAA'>",
name="test_param <value=AAA>",
tags={"symbol": {"aaa"}},
),
TestCaseReport(
name="test_param <value='BBB'>",
name="test_param <value=BBB>",
tags={"symbol": {"bbb"}},
),
],
Expand All @@ -108,8 +108,8 @@ def test_param_2(self, env, result, value):
category="parametrization",
tags={"speed": {"fast"}},
entries=[
TestCaseReport(name="test_param_2 <value='XXX'>"),
TestCaseReport(name="test_param_2 <value='YYY'>"),
TestCaseReport(name="test_param_2 <value=XXX>"),
TestCaseReport(name="test_param_2 <value=YYY>"),
],
),
],
Expand Down Expand Up @@ -158,11 +158,11 @@ def test_param_2(self, env, result, value):
tags={"speed": {"slow"}},
entries=[
TestCaseReport(
name="test_param <value='AAA'>",
name="test_param <value=AAA>",
tags={"symbol": {"aaa"}},
),
TestCaseReport(
name="test_param <value='BBB'>",
name="test_param <value=BBB>",
tags={"symbol": {"bbb"}},
),
],
Expand All @@ -172,8 +172,8 @@ def test_param_2(self, env, result, value):
category="parametrization",
tags={"speed": {"fast"}},
entries=[
TestCaseReport(name="test_param_2 <value='XXX'>"),
TestCaseReport(name="test_param_2 <value='YYY'>"),
TestCaseReport(name="test_param_2 <value=XXX>"),
TestCaseReport(name="test_param_2 <value=YYY>"),
],
),
],
Expand Down

0 comments on commit 6a0cddb

Please sign in to comment.