diff --git a/src/.DS_Store b/src/.DS_Store new file mode 100644 index 000000000..112d642aa Binary files /dev/null and b/src/.DS_Store differ diff --git a/src/_pytest/mark/structures.py b/src/_pytest/mark/structures.py index a6503bf1d..c2057e75d 100644 --- a/src/_pytest/mark/structures.py +++ b/src/_pytest/mark/structures.py @@ -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. diff --git a/src/_pytest/python.py b/src/_pytest/python.py index 5e059f2c4..9238694c3 100644 --- a/src/_pytest/python.py +++ b/src/_pytest/python.py @@ -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}, )'" + f"\n\n" + f"Or if multiple parameters are used, separate them by commas. \n " + f"e.g., '@pytest.mark.parametrize(\"arg1, arg2\", )'", pytrace=False, ) arg_directness[arg] = "indirect" diff --git a/testing/test_mark.py b/testing/test_mark.py index 2896afa45..f9eee67cc 100644 --- a/testing/test_mark.py +++ b/testing/test_mark.py @@ -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\", )'", + "", + "Or if multiple parameters are used, separate them by commas. ", + " e.g., '@pytest.mark.parametrize(\"arg1, arg2\", )'", + ] + ) + + 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"', ] )