Skip to content

Commit d470fae

Browse files
authored
Make tests just a runtime warning (#37)
1 parent d12ae43 commit d470fae

File tree

3 files changed

+40
-29
lines changed

3 files changed

+40
-29
lines changed

pyproject.toml

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,7 @@ description = "PyTest Plus Plugin :: extends pytest functionality"
3333
dynamic = ["version", "dependencies", "optional-dependencies"]
3434
keywords = ["testing", "pytest", "plugin"]
3535
license = {text = "MIT"}
36-
maintainers = [
37-
{"email" = "[email protected]", "name" = "Sorin Sbarnea"}
38-
]
36+
maintainers = [{"email" = "[email protected]", "name" = "Sorin Sbarnea"}]
3937
name = "pytest-plus"
4038
readme = "README.md"
4139
# https://peps.python.org/pep-0621/#readme
@@ -58,7 +56,7 @@ show_missing = true
5856
omit = ["test/*", "/private/var/folders/*", "/tmp/*"]
5957

6058
[tool.pytest.ini_options]
61-
addopts = "-p no:flaky"
59+
addopts = "-p no:flaky --showlocals"
6260
filterwarnings = [
6361
"error",
6462
"ignore:ast.(Str|Num|NameConstant) is deprecated and will be removed in Python 3.14:DeprecationWarning:_pytest.assertion.rewrite",

src/pytest_plus/__init__.py

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@
22

33
from __future__ import annotations
44

5+
import logging
56
import os
67
import re
78
import shutil
89

910
import pytest
1011
from _pytest.terminal import TerminalReporter
1112

13+
_logger = logging.getLogger(__name__)
1214
PYTEST_CHECK_TEST_DUPLICATE = int(os.environ.get("PYTEST_CHECK_TEST_DUPLICATE", "1"))
1315

1416

@@ -61,7 +63,7 @@ def pytest_sessionfinish(session: pytest.Session) -> None:
6163
@pytest.hookimpl(tryfirst=True) # type: ignore[misc,unused-ignore]
6264
def pytest_collection_modifyitems(items: list[pytest.Item]) -> None:
6365
"""Ensure testing fails if tests have duplicate names."""
64-
errors = []
66+
messages = []
6567
names = {}
6668
max_test_id_length = get_max_test_id_length()
6769
test_id_regex = get_test_id_regex()
@@ -70,21 +72,18 @@ def pytest_collection_modifyitems(items: list[pytest.Item]) -> None:
7072
if base_name not in names:
7173
names[base_name] = item.location
7274
elif item.location[:2] != names[base_name][:2] and PYTEST_CHECK_TEST_DUPLICATE:
73-
error = f"Duplicate test name '{base_name}', found at {item.location[0]}:{item.location[1]} and {names[base_name][0]}:{names[base_name][1]}"
74-
if error not in errors:
75-
errors.append(error)
75+
msg = f"Duplicate test name '{base_name}', found at {item.location[0]}:{item.location[1]} and {names[base_name][0]}:{names[base_name][1]}"
76+
if msg not in messages:
77+
messages.append(msg)
7678
if hasattr(item, "callspec"):
7779
test_id = item.callspec.id
7880
if max_test_id_length and len(test_id) > max_test_id_length:
79-
errors.append(
81+
messages.append(
8082
f"{item} has an id that looks above {max_test_id_length} characters.",
8183
)
8284
elif test_id_regex and not test_id_regex.match(test_id):
83-
errors.append(
85+
messages.append(
8486
f"Test {item} has an id that does not match our safe pattern '{test_id_regex.pattern}' for use with a terminal.",
8587
)
86-
if errors:
87-
msg = f"Failed run due to following issues being identified:\n{os.linesep.join(errors)}\nTotal errors found: {len(errors)}"
88-
raise pytest.UsageError(
89-
msg,
90-
)
88+
for msg in messages:
89+
_logger.warning(msg)

test/test_plugin.py

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,21 @@
11
"""Tests."""
22

3+
import logging
34
import os
45

56
import pytest
67

78
pytest_plugins = ["pytester"]
89

910

10-
def test_duplicate_test_name(pytester: pytest.Pytester) -> None:
11+
def test_duplicate_test_name(
12+
pytester: pytest.Pytester,
13+
caplog: pytest.LogCaptureFixture,
14+
monkeypatch: pytest.MonkeyPatch,
15+
) -> None:
1116
"""Validates that we can detect duplicate test names."""
17+
monkeypatch.delenv("PYTEST_REQPASS", raising=False)
18+
caplog.set_level(logging.INFO)
1219
p1 = pytester.makepyfile(
1320
test_one="""
1421
def test_a():
@@ -24,25 +31,28 @@ def test_a():
2431

2532
result = pytester.runpytest_inprocess(p1, p2)
2633
assert (
27-
result.errlines[0]
28-
== "ERROR: Failed run due to following issues being identified:"
34+
"Duplicate test name 'test_a', found at test_two.py:0 and test_one.py:0"
35+
in caplog.text
2936
)
30-
assert (
31-
result.errlines[1]
32-
== "Duplicate test name 'test_a', found at test_two.py:0 and test_one.py:0"
33-
)
34-
assert result.ret == pytest.ExitCode.USAGE_ERROR
37+
assert result.ret == 0, result.stdout.lines
3538

3639

3740
@pytest.mark.parametrize(
3841
("rc", "disable"),
3942
[
40-
pytest.param(pytest.ExitCode.USAGE_ERROR, False, id="0"),
43+
pytest.param(pytest.ExitCode.OK, False, id="0"),
4144
pytest.param(pytest.ExitCode.OK, True, id="1"),
4245
],
4346
)
44-
def test_check_test_id(pytester: pytest.Pytester, rc: int, *, disable: bool) -> None:
47+
def test_check_test_id(
48+
pytester: pytest.Pytester,
49+
rc: int,
50+
*,
51+
disable: bool,
52+
caplog: pytest.LogCaptureFixture,
53+
) -> None:
4554
"""Validates that we can detect duplicate test names."""
55+
caplog.set_level(logging.WARNING)
4656
if disable:
4757
os.environ["PYTEST_CHECK_TEST_ID_REGEX"] = "0"
4858
p1 = pytester.makepyfile(
@@ -62,13 +72,17 @@ def test_a(some: str):
6272
if not disable:
6373
assert (
6474
"Test <Function test_a[invalid name]> has an id that does not match our safe pattern '^[\\w_\\-\\.:]+$' for use with a terminal."
65-
in result.stderr.lines
75+
in caplog.text
6676
)
6777
assert result.ret == rc
6878

6979

70-
def test_check_test_id_length(pytester: pytest.Pytester) -> None:
80+
def test_check_test_id_length(
81+
pytester: pytest.Pytester,
82+
caplog: pytest.LogCaptureFixture,
83+
) -> None:
7184
"""Validates that we can detect duplicate test names."""
85+
caplog.set_level(logging.WARNING)
7286
p1 = pytester.makepyfile(
7387
test_one="""
7488
import pytest
@@ -85,6 +99,6 @@ def test_a(some: str):
8599
result = pytester.runpytest_inprocess("--collect-only", p1)
86100
assert (
87101
"<Function test_a[this-is-too-long-for-our-taste-so-we-ask-you-to-make-it-shorter]> has an id that looks above 60 characters."
88-
in result.stderr.lines
102+
in caplog.text
89103
)
90-
assert result.ret == pytest.ExitCode.USAGE_ERROR
104+
assert result.ret == pytest.ExitCode.OK

0 commit comments

Comments
 (0)