Wie kann ich Python Decorator Factory @pyTest.mark.Paramerize an eine importierte Funktion anbringen?
Posted: 25 Aug 2025, 16:18
In meiner Testsuite habe ich viel Code wie diesen: < /p>
Anstatt test_wrong_number_of_argumente oft zu definieren, möchte ich dieses einmal in Helfer definieren und das in jeder Datei dekorieren. Wenden Sie einen Dekorator auf eine importierte Funktion an?, aber die Herausforderung hier ist die Selbstbeobachtung von PyTest. Wenn ich versuche: < /p>
und führen Sie PyTest aus:
Code: Select all
# file test/test_procedural.py
from test.helper import check_evaluation
@pytest.mark.parametrize(
("str_expr", "msgs", "assert_fail_msg"),
[
(
"Abort[a, b]",
["Abort called with 2 arguments; 0 arguments are expected."],
"Abort argument error call",
),
(
"Catch[a, b, c, d, e]",
["Catch called with 5 arguments; between 1 and 3 arguments are expected."],
"Catch argument error call",
),
# ...
],
)
def test_wrong_number_of_arguments(str_expr, msgs, assert_fail_msg):
check_evaluation(
str_expr,
str_expr,
to_string_expr=True,
to_string_expected=True,
hold_expected=True,
failure_message=assert_fail_msg,
expected_messages=msgs,
)
# file test/test_strings.py
from test.helper import check_evaluation
@pytest.mark.parametrize(
("str_expr", "msgs", "assert_fail_msg"),
[
(
"ToExpression[]",
(
"ToExpression called with 0 arguments; between 1 and 3 arguments are expected.",
),
"ToExpression argument error call",
),
],
)
def test_wrong_number_of_arguments(str_expr, msgs, assert_fail_msg):
check_evaluation(
str_expr,
str_expr,
to_string_expr=True,
to_string_expected=True,
hold_expected=True,
failure_message=assert_fail_msg,
expected_messages=msgs,
)
Code: Select all
from test.helper import wrong_number_of_arguments
test_wrong_number_of_arguments = pytest.mark.parametrize(wrong_number_of_arguments,
("str_expr", "msgs", "assert_fail_msg"),
[
(
"ToExpression[]",
(
"ToExpression called with 0 arguments; between 1 and 3 arguments are expected.",
),
"ToExpression argument error call",
),
],
)
Code: Select all
/tmp/3.13.7/lib/python3.13/site-packages/_pytest/mark/structures.py:391: PytestCollectionWarning: cannot collect 'test_wrong_number_of_arguments' because it is not a function.
def __call__(self, *args: object, **kwargs: object):