Skip to content

Commit abcde0d

Browse files
committed
feat: add a hook to error out on no test runs
1 parent 976c0bc commit abcde0d

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

examples/conftest.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import os
22
import random
3+
import pytest
34

45
os.environ["PYTHONHASHSEED"] = "0"
56

@@ -18,3 +19,26 @@
1819
pass
1920
else:
2021
np.random.seed(0)
22+
23+
24+
def pytest_terminal_summary(terminalreporter, exitstatus, config):
25+
"""Ensure that at least one test is collected. Error out if all tests are skipped."""
26+
known_types = {
27+
"failed",
28+
"passed",
29+
"skipped",
30+
"deselected",
31+
"xfailed",
32+
"xpassed",
33+
"warnings",
34+
"error",
35+
}
36+
if (sum(
37+
len(terminalreporter.stats.get(k, []))
38+
for k in known_types.difference({"skipped", "deselected"})) == 0):
39+
terminalreporter.write_sep(
40+
"!",
41+
(f"Error: No tests were collected. "
42+
f"{dict(sorted((k, len(v)) for k, v in terminalreporter.stats.items()))}"),
43+
)
44+
pytest.exit("No tests were collected.", returncode=5)

testing/conftest.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import os
22
import random
3+
import pytest
34

45
os.environ["PYTHONHASHSEED"] = "0"
56

@@ -18,3 +19,26 @@
1819
pass
1920
else:
2021
np.random.seed(0)
22+
23+
24+
def pytest_terminal_summary(terminalreporter, exitstatus, config):
25+
"""Ensure that at least one test is collected. Error out if all tests are skipped."""
26+
known_types = {
27+
"failed",
28+
"passed",
29+
"skipped",
30+
"deselected",
31+
"xfailed",
32+
"xpassed",
33+
"warnings",
34+
"error",
35+
}
36+
if (sum(
37+
len(terminalreporter.stats.get(k, []))
38+
for k in known_types.difference({"skipped", "deselected"})) == 0):
39+
terminalreporter.write_sep(
40+
"!",
41+
(f"Error: No tests were collected. "
42+
f"{dict(sorted((k, len(v)) for k, v in terminalreporter.stats.items()))}"),
43+
)
44+
pytest.exit("No tests were collected.", returncode=5)

0 commit comments

Comments
 (0)