From df8fe670f029a974d8f4cabb8cf499c9ac320561 Mon Sep 17 00:00:00 2001 From: "John T. Wodder II" Date: Fri, 12 Aug 2022 21:34:40 -0400 Subject: [PATCH] Error if @fail_slow is not given exactly one argument --- CHANGELOG.md | 6 ++++++ src/pytest_fail_slow.py | 8 ++++++-- test/test_fail_slow.py | 19 +++++++++++++++++++ 3 files changed, 31 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 857cf38..acf21fa 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,9 @@ +v0.3.0 (in development) +----------------------- +- The `@pytest.mark.fail_slow()` marker now errors if not given exactly one + argument. Previously, it would either use the first argument or, if no + arguments were given, it would be ignored. + v0.2.0 (2022-04-25) ------------------- - Test against pytest 7 diff --git a/src/pytest_fail_slow.py b/src/pytest_fail_slow.py index c07e068..6d7fdd4 100644 --- a/src/pytest_fail_slow.py +++ b/src/pytest_fail_slow.py @@ -44,7 +44,7 @@ Visit for more information. """ -__version__ = "0.2.0" +__version__ = "0.3.0.dev1" __author__ = "John Thorvald Wodder II" __author_email__ = "pytest-fail-slow@varonathe.org" __license__ = "MIT" @@ -109,7 +109,11 @@ def pytest_addoption(parser) -> None: def pytest_runtest_makereport(item, call): report = (yield).get_result() mark = item.get_closest_marker("fail_slow") - if mark is not None and len(mark.args) > 0: + if mark is not None: + if len(mark.args) != 1: + raise pytest.UsageError( + "@pytest.mark.fail_slow() takes exactly one argument" + ) timeout = parse_duration(mark.args[0]) else: timeout = item.config.getoption("--fail-slow") diff --git a/test/test_fail_slow.py b/test/test_fail_slow.py index 28d5fde..6187989 100644 --- a/test/test_fail_slow.py +++ b/test/test_fail_slow.py @@ -99,3 +99,22 @@ def test_fail_slow_threshold( ) else: result.stdout.no_fnmatch_line("*Test passed but took too long to run*") + + +@pytest.mark.parametrize("args", ["", "42, 'foo'"]) +def test_fail_slow_marker_bad_args(pytester, args: str) -> None: + pytester.makepyfile( + test_func=( + "import pytest\n" + "\n" + f"@pytest.mark.fail_slow({args})\n" + "def test_func():\n" + " assert 2 + 2 == 4\n" + ) + ) + result = pytester.runpytest() + result.assert_outcomes() + result.stdout.no_fnmatch_line("?") + result.stderr.fnmatch_lines( + ["ERROR: @pytest.mark.fail_slow() takes exactly one argument"] + )