Merge pull request #2 from Yunirang/dev1

Merging improved error logged messages for parameterized testing
This commit is contained in:
HFFuture 2024-04-28 21:46:22 -04:00 committed by GitHub
commit 61dc40c080
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 74 additions and 21 deletions

BIN
src/.DS_Store vendored Normal file

Binary file not shown.

View File

@ -165,22 +165,20 @@ class ParameterSet(NamedTuple):
# Check all parameter sets have the correct number of values.
for param in parameters:
if len(param.values) != len(argnames):
# Construct a string representation of the expected parameter names
expected_argnames = ", ".join(argnames)
# Construct a string representation of the provided parameter values
provided_values = ", ".join(map(repr, param.values))
msg = (
'{nodeid}: in "parametrize" the number of names ({names_len}):\n'
" {names}\n"
"must be equal to the number of values ({values_len}):\n"
" {values}"
)
fail(
msg.format(
nodeid=nodeid,
values=param.values,
names=argnames,
names_len=len(argnames),
values_len=len(param.values),
),
pytrace=False,
f'{nodeid}: \n\n Error in parameterization for test "{func.__name__}".\n '
f"The number of specified parameters ({len(argnames)}) "
f"does not match the number of provided values ({len(param.values)}): {provided_values}. \n"
f" Please ensure that the correct number of parameter names "
f"({len(param.values)}) are separated by commas within quotes.\n\n"
f'Require more than parameter names: "{expected_argnames}"'
)
fail(msg, pytrace=False)
else:
# Empty parameter set (likely computed at runtime): create a single
# parameter set with NOTSET values, with the "empty parameter set" mark applied to it.

View File

@ -1371,8 +1371,15 @@ class Metafunc:
# num_ids == 0 is a special case: https://github.com/pytest-dev/pytest/issues/1849
if num_ids != len(parametersets) and num_ids != 0:
msg = "In {}: {} parameter sets specified, with different number of ids: {}"
fail(msg.format(func_name, len(parametersets), num_ids), pytrace=False)
# Construct a string representation of the expected parameter sets
expected_paramsets = len(parametersets)
msg = (
f"In {func_name}: \n\n"
f" Specified {expected_paramsets} parameter sets with {num_ids} different ids. \n"
f" Ensure all subparameters are correctly grouped within a single set of quotation marks."
)
fail(msg, pytrace=False)
return list(itertools.islice(ids, num_ids))
@ -1402,8 +1409,24 @@ class Metafunc:
arg_directness = dict.fromkeys(argnames, "direct")
for arg in indirect:
if arg not in argnames:
# Construct a list of valid parameter names
valid_params = ", ".join([f'"{name}"' for name in argnames])
# Construct a string representing the expected number of parameters
expected_param_count = len(indirect[0])
# Construct a string representing the actual number of parameters provided
actual_param_count = len(argnames)
fail(
f"In {self.function.__name__}: indirect fixture '{arg}' doesn't exist",
f"In function {self.function.__name__}: {argnames} is not a valid parameter. \n"
f"Expected {expected_param_count} sub parameters, "
f"but only {actual_param_count} were provided. \n\n"
f"Make sure to pass parameter names as strings without quotes, separated by commas, \n "
f"e.g., '@pytest.mark.parametrize({valid_params}, <Input Values>)'"
f"\n\n"
f"Or if multiple parameters are used, separate them by commas. \n "
f"e.g., '@pytest.mark.parametrize(\"arg1, arg2\", <Input Tuples>)'",
pytrace=False,
)
arg_directness[arg] = "indirect"

View File

@ -404,6 +404,35 @@ def test_parametrized_collected_from_command_line(pytester: Pytester) -> None:
rec.assertoutcome(passed=3)
def test_parametrized_collect_with_wrong_format(pytester: Pytester) -> None:
"""Parametrized test argument format not intuitive
line issue#8593"""
py_file = pytester.makepyfile(
"""
import pytest
@pytest.mark.parametrize("arg1", "arg2", [(1, 1)])
def test_parametrization(arg1: int, arg2: int) -> None:
assert arg1 == arg2
assert arg1 + 1 == arg2 + 1
"""
)
result = pytester.runpytest(py_file)
result.stdout.fnmatch_lines(
[
"In function test_parametrization: ['arg1'] is not a valid parameter. ",
"Expected 2 sub parameters, but only 1 were provided. ",
"",
"Make sure to pass parameter names as strings without quotes, separated by commas, ",
" e.g., '@pytest.mark.parametrize(\"arg1\", <Input Values>)'",
"",
"Or if multiple parameters are used, separate them by commas. ",
" e.g., '@pytest.mark.parametrize(\"arg1, arg2\", <Input Tuples>)'",
]
)
def test_parametrized_collect_with_wrong_args(pytester: Pytester) -> None:
"""Test collect parametrized func with wrong number of args."""
py_file = pytester.makepyfile(
@ -419,10 +448,13 @@ def test_parametrized_collect_with_wrong_args(pytester: Pytester) -> None:
result = pytester.runpytest(py_file)
result.stdout.fnmatch_lines(
[
'test_parametrized_collect_with_wrong_args.py::test_func: in "parametrize" the number of names (2):',
" ['foo', 'bar']",
"must be equal to the number of values (3):",
" (1, 2, 3)",
"test_parametrized_collect_with_wrong_args.py::test_func: ",
"",
' Error in parameterization for test "test_func".',
" The number of specified parameters (2) does not match the number of provided values (3): 1, 2, 3. ",
" Please ensure that the correct number of parameter names (3) are separated by commas within quotes.",
"",
'Require more than parameter names: "foo, bar"',
]
)