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. # Check all parameter sets have the correct number of values.
for param in parameters: for param in parameters:
if len(param.values) != len(argnames): 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 = ( msg = (
'{nodeid}: in "parametrize" the number of names ({names_len}):\n' f'{nodeid}: \n\n Error in parameterization for test "{func.__name__}".\n '
" {names}\n" f"The number of specified parameters ({len(argnames)}) "
"must be equal to the number of values ({values_len}):\n" f"does not match the number of provided values ({len(param.values)}): {provided_values}. \n"
" {values}" f" Please ensure that the correct number of parameter names "
) f"({len(param.values)}) are separated by commas within quotes.\n\n"
fail( f'Require more than parameter names: "{expected_argnames}"'
msg.format(
nodeid=nodeid,
values=param.values,
names=argnames,
names_len=len(argnames),
values_len=len(param.values),
),
pytrace=False,
) )
fail(msg, pytrace=False)
else: else:
# Empty parameter set (likely computed at runtime): create a single # Empty parameter set (likely computed at runtime): create a single
# parameter set with NOTSET values, with the "empty parameter set" mark applied to it. # 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 # num_ids == 0 is a special case: https://github.com/pytest-dev/pytest/issues/1849
if num_ids != len(parametersets) and num_ids != 0: if num_ids != len(parametersets) and num_ids != 0:
msg = "In {}: {} parameter sets specified, with different number of ids: {}" # Construct a string representation of the expected parameter sets
fail(msg.format(func_name, len(parametersets), num_ids), pytrace=False) 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)) return list(itertools.islice(ids, num_ids))
@ -1402,8 +1409,24 @@ class Metafunc:
arg_directness = dict.fromkeys(argnames, "direct") arg_directness = dict.fromkeys(argnames, "direct")
for arg in indirect: for arg in indirect:
if arg not in argnames: 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( 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, pytrace=False,
) )
arg_directness[arg] = "indirect" arg_directness[arg] = "indirect"

View File

@ -404,6 +404,35 @@ def test_parametrized_collected_from_command_line(pytester: Pytester) -> None:
rec.assertoutcome(passed=3) 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: def test_parametrized_collect_with_wrong_args(pytester: Pytester) -> None:
"""Test collect parametrized func with wrong number of args.""" """Test collect parametrized func with wrong number of args."""
py_file = pytester.makepyfile( py_file = pytester.makepyfile(
@ -419,10 +448,13 @@ def test_parametrized_collect_with_wrong_args(pytester: Pytester) -> None:
result = pytester.runpytest(py_file) result = pytester.runpytest(py_file)
result.stdout.fnmatch_lines( result.stdout.fnmatch_lines(
[ [
'test_parametrized_collect_with_wrong_args.py::test_func: in "parametrize" the number of names (2):', "test_parametrized_collect_with_wrong_args.py::test_func: ",
" ['foo', 'bar']", "",
"must be equal to the number of values (3):", ' Error in parameterization for test "test_func".',
" (1, 2, 3)", " 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"',
] ]
) )