Skip to content

~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.

chore(hooks): add check-weak-tests to detect tautological / no-op tests (#4372)

LearningCircuit09c702b27846
diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml
index a96d5c53cd..37e3358f9b 100644
--- a/.pre-commit-config.yaml
+++ b/.pre-commit-config.yaml
@@ -369,6 +369,14 @@ repos:
files: ^tests/.*\.py$
description: "Warn when test files redefine app/client/authenticated_client
fixtures from root conftest.py"
+ - id: check-weak-tests
+ name: Check for tautological / no-op tests
+ entry: .pre-commit-hooks/check-weak-tests.py
+ language: script
+ files: ^tests/.*\.py$
+ description: "Block tests that pass regardless of the SUT: assert-True-only,
+ import-existence tautologies, `... or True`, and
+ `x is None or x is not None`. Opt out per-test with `# allow: weak-test`."
# Hooks intentionally NOT added
# See docs/decisions/0002-pre-commit-hook-reviews.md for full rationale.
diff --git a/.pre-commit-hooks/check-weak-tests.py b/.pre-commit-hooks/check-weak-tests.py
new file mode 100755
index 0000000000..963dbf3099
--- /dev/null
+++ b/.pre-commit-hooks/check-weak-tests.py
@@ -0,0 +1,227 @@
+#!/usr/bin/env python3
+"""
+Pre-commit hook to detect tautological / no-op test functions.
+
+These patterns pass regardless of whether the system-under-test (SUT) works,
+so they provide false confidence and inflate coverage without catching
+regressions. A 2026 test-suite audit removed ~3,700 such tests
+(see docs/processes/test-review/README.md); this hook prevents reintroduction.
+
+Patterns flagged (each is *never* a legitimate assertion):
+
+ 1. ASSERT_TRUE — a test whose only assertion(s) are ``assert True``
+ (or ``assert <constant-truthy-literal>``). Verifies nothing.
+
+ 2. IMPORT_EXISTENCE — body is exactly ``from M import X`` + ``assert X is
+ not None``. The import already raises ImportError if X is missing, so
+ the assert pins nothing the import didn't already.
+
+ 3. TAUTOLOGY_OR — ``assert <anything> or True`` (always True).
+
+ 4. NULLCHECK_TAUTOLOGY — ``assert x is None or x is not None`` (or the
+ reverse ordering) on the same operand. Accepts every possible value.
+
+Opt-out: add ``# allow: weak-test`` on the ``def`` line or the assert line.
+Use sparingly and only with a comment explaining why the pattern is
+intentional (e.g. a deliberately-minimal smoke test).
+"""
+
+import ast
+import re
+import sys
+
+SUPPRESS_RE = re.compile(r"#\s*allow:\s*weak-test(?:\s|$)")
+
+
+def _strip_docstring(body: list[ast.stmt]) -> list[ast.stmt]:
+ if (
+ body
+ and isinstance(body[0], ast.Expr)
+ and isinstance(body[0].value, ast.Constant)
+ and isinstance(body[0].value.value, str)
+ ):
+ return body[1:]
+ return body
+
+
+def _is_truthy_constant(node: ast.expr) -> bool:
+ """True for ``True`` / non-empty literal constants that are always truthy."""
+ return isinstance(node, ast.Constant) and bool(node.value)
+
+
+def _is_none(node: ast.expr) -> bool:
+ return isinstance(node, ast.Constant) and node.value is None
+
+
+def _operand_src(node: ast.expr) -> str:
+ try:
+ return ast.unparse(node)
+ except Exception: # pragma: no cover - unparse is stable on 3.9+
+ return repr(node)
+
+
+def _is_nullcheck_tautology(test: ast.expr) -> bool:
+ """Detect ``x is None or x is not None`` (either ordering, same operand)."""
+ if not (isinstance(test, ast.BoolOp) and isinstance(test.op, ast.Or)):
+ return False
+ if len(test.values) != 2:
+ return False
+ left, right = test.values
+ if not (isinstance(left, ast.Compare) and isinstance(right, ast.Compare)):
+ return False
+ if not (len(left.ops) == 1 and len(right.ops) == 1):
+ return False
+ left_is = isinstance(left.ops[0], ast.Is)
+ left_isnot = isinstance(left.ops[0], ast.IsNot)
+ right_is = isinstance(right.ops[0], ast.Is)
+ right_isnot = isinstance(right.ops[0], ast.IsNot)
+ # Both comparisons must be against None
+ if not (_is_none(left.comparators[0]) and _is_none(right.comparators[0])):
+ return False
+ # Same operand on both sides
+ if _operand_src(left.left) != _operand_src(right.left):
+ return False
+ # One "is None" and one "is not None" => covers all values
+ return (left_is and right_isnot) or (left_isnot and right_is)
+
+
+def _has_or_true(test: ast.expr) -> bool:
+ """Detect ``<anything> or True`` (or ``True or <anything>``)."""
+ if not (isinstance(test, ast.BoolOp) and isinstance(test.op, ast.Or)):
+ return False
+ return any(_is_truthy_constant(v) for v in test.values)
+
+
+class WeakTestChecker(ast.NodeVisitor):
+ def __init__(self, filepath: str, lines: list[str]):
+ self.filepath = filepath
+ self.lines = lines
+ self.issues: list[tuple[int, str]] = []
+
+ def _suppressed(self, lineno: int) -> bool:
+ idx = lineno - 1
+ return 0 <= idx < len(self.lines) and bool(
+ SUPPRESS_RE.search(self.lines[idx])
+ )
+
+ def _has_skip_marker(self, node: ast.FunctionDef) -> bool:
+ for dec in node.decorator_list:
+ src = _operand_src(dec)
+ if "skip" in src or "xfail" in src:
+ return True
+ return False
+
+ def visit_FunctionDef(self, node: ast.FunctionDef) -> None:
+ if node.name.startswith("test_") and not self._has_skip_marker(node):
+ self._check_test(node)
+ self.generic_visit(node)
+
+ def _check_test(self, node: ast.FunctionDef) -> None:
+ # Function-level opt-out (on the def line).
+ if self._suppressed(node.lineno):
+ return
+
+ body = _strip_docstring(node.body)
+ asserts = [s for s in body if isinstance(s, ast.Assert)]
+
+ # Pattern 3 & 4: per-assert tautologies (anywhere in the body).
+ for a in asserts:
+ if self._suppressed(a.lineno):
+ continue
+ if _has_or_true(a.test):
+ self.issues.append(
+ (a.lineno, "assertion is always true (`... or True`)")
+ )
+ elif _is_nullcheck_tautology(a.test):
+ self.issues.append(
+ (
+ a.lineno,
+ "assertion `x is None or x is not None` accepts any value",
+ )
+ )
+
+ if not asserts:
+ # Pattern 2: import-existence tautology (no Assert means it can't
+ # match here; handled below only when an assert exists).
+ return
+
+ # Pattern 1: every assertion is a truthy literal (e.g. `assert True`).
+ if all(_is_truthy_constant(a.test) for a in asserts):
+ if not self._suppressed(node.lineno):
+ self.issues.append(
+ (
+ node.lineno,
+ f"test '{node.name}' has no real assertion "
+ "(only `assert <truthy-literal>`)",
+ )
+ )
+
+ # Pattern 2: body is exactly import(s) + `assert X is not None`.
+ non_import = [
+ s for s in body if not isinstance(s, (ast.Import, ast.ImportFrom))
+ ]
+ if (
+ len(non_import) == 1
+ and isinstance(non_import[0], ast.Assert)
+ and len(body) > 1 # at least one import present
+ ):
+ test = non_import[0].test
+ if (
+ isinstance(test, ast.Compare)
+ and len(test.ops) == 1
+ and isinstance(test.ops[0], ast.IsNot)
+ and _is_none(test.comparators[0])
+ ):
+ if not self._suppressed(node.lineno):
+ self.issues.append(
+ (
+ node.lineno,
+ f"test '{node.name}' is an import-existence "
+ "tautology (the import already fails if the "
+ "symbol is missing)",
+ )
+ )
+
+
+def check_file(filepath: str) -> list[tuple[int, str]]:
+ try:
+ with open(filepath, encoding="utf-8") as fh:
+ source = fh.read()
+ except (OSError, UnicodeDecodeError):
+ return []
+ try:
+ tree = ast.parse(source)
+ except SyntaxError:
+ # Leave syntax errors to other tools (ruff / py_compile).
+ return []
+ checker = WeakTestChecker(filepath, source.splitlines())
+ checker.visit(tree)
+ return sorted(checker.issues)
+
+
+def main(argv: list[str]) -> int:
+ files = [
+ f
+ for f in argv
+ if f.endswith(".py")
+ and ("/test_" in f or f.startswith("test_") or "/tests/" in f)
+ ]
+ found = False
+ for filepath in files:
+ for lineno, message in check_file(filepath):
+ found = True
+ print(f"{filepath}:{lineno}: {message}")
+ if found:
+ print(
+ "\nWeak/tautological test patterns detected (see above).\n"
+ "These pass regardless of whether the code works. Either assert a "
+ "real outcome, or — if the no-op is intentional (e.g. a minimal "
+ "smoke test) — add `# allow: weak-test` with a justifying comment.\n"
+ "Background: docs/processes/test-review/README.md"
+ )
+ return 1
+ return 0
+
+
+if __name__ == "__main__":
+ sys.exit(main(sys.argv[1:]))
diff --git a/tests/hooks/test_check_weak_tests.py b/tests/hooks/test_check_weak_tests.py
new file mode 100644
index 0000000000..68842d3d87
--- /dev/null
+++ b/tests/hooks/test_check_weak_tests.py
@@ -0,0 +1,178 @@
+"""Tests for the check-weak-tests pre-commit hook."""
+
+import importlib.util
+from pathlib import Path
+
+# Load the hook module by path (hyphenated filename isn't importable directly).
+_HOOK_PATH = (
+ Path(__file__).resolve().parents[2]
+ / ".pre-commit-hooks"
+ / "check-weak-tests.py"
+)
+_spec = importlib.util.spec_from_file_location("check_weak_tests", _HOOK_PATH)
+check_weak_tests = importlib.util.module_from_spec(_spec)
+_spec.loader.exec_module(check_weak_tests)
+
+
+def _write(tmp_path, body: str) -> str:
+ fp = tmp_path / "test_sample.py"
+ fp.write_text(body)
+ return str(fp)
+
+
+class TestAssertTrueOnly:
+ def test_flags_assert_true_only(self, tmp_path):
+ fp = _write(
+ tmp_path,
+ "def test_x():\n do_thing()\n assert True\n",
+ )
+ issues = check_weak_tests.check_file(fp)
+ assert len(issues) == 1
+ assert "no real assertion" in issues[0][1]
+
+ def test_real_assertion_not_flagged(self, tmp_path):
+ fp = _write(
+ tmp_path,
+ "def test_x():\n assert compute() == 42\n",
+ )
+ assert check_weak_tests.check_file(fp) == []
+
+ def test_assert_true_alongside_real_assert_not_flagged(self, tmp_path):
+ # If a real assertion exists, the test is not a pure no-op.
+ fp = _write(
+ tmp_path,
+ "def test_x():\n assert compute() == 1\n assert True\n",
+ )
+ assert check_weak_tests.check_file(fp) == []
+
+
+class TestImportExistence:
+ def test_flags_import_existence(self, tmp_path):
+ fp = _write(
+ tmp_path,
+ "def test_x():\n from mymod import thing\n"
+ " assert thing is not None\n",
+ )
+ issues = check_weak_tests.check_file(fp)
+ assert len(issues) == 1
+ assert "import-existence tautology" in issues[0][1]
+
+ def test_import_then_real_use_not_flagged(self, tmp_path):
+ fp = _write(
+ tmp_path,
+ "def test_x():\n from mymod import thing\n"
+ " assert thing() == 5\n",
+ )
+ assert check_weak_tests.check_file(fp) == []
+
+
+class TestOrTrueTautology:
+ def test_flags_or_true(self, tmp_path):
+ fp = _write(
+ tmp_path,
+ "def test_x():\n result = run()\n assert result or True\n",
+ )
+ issues = check_weak_tests.check_file(fp)
+ assert len(issues) == 1
+ assert "always true" in issues[0][1]
+
+
+class TestNullcheckTautology:
+ def test_flags_is_none_or_is_not_none(self, tmp_path):
+ fp = _write(
+ tmp_path,
+ "def test_x():\n r = run()\n"
+ " assert r is None or r is not None\n",
+ )
+ issues = check_weak_tests.check_file(fp)
+ assert len(issues) == 1
+ assert "accepts any value" in issues[0][1]
+
+ def test_flags_reversed_ordering(self, tmp_path):
+ fp = _write(
+ tmp_path,
+ "def test_x():\n r = run()\n"
+ " assert r is not None or r is None\n",
+ )
+ assert len(check_weak_tests.check_file(fp)) == 1
+
+ def test_different_operands_not_flagged(self, tmp_path):
+ # `a is None or b is not None` is a real (if odd) condition.
+ fp = _write(
+ tmp_path,
+ "def test_x():\n assert a is None or b is not None\n",
+ )
+ assert check_weak_tests.check_file(fp) == []
+
+
+class TestOptOut:
+ def test_allow_comment_on_def_line(self, tmp_path):
+ fp = _write(
+ tmp_path,
+ "def test_x(): # allow: weak-test\n assert True\n",
+ )
+ assert check_weak_tests.check_file(fp) == []
+
+ def test_allow_comment_on_assert_line(self, tmp_path):
+ fp = _write(
+ tmp_path,
+ "def test_x():\n r = run()\n"
+ " assert r or True # allow: weak-test\n",
+ )
+ assert check_weak_tests.check_file(fp) == []
+
+
+class TestSkipMarkers:
+ def test_skip_decorated_test_not_flagged(self, tmp_path):
+ fp = _write(
+ tmp_path,
+ "import pytest\n\n"
+ "@pytest.mark.skip(reason='wip')\n"
+ "def test_x():\n assert True\n",
+ )
+ assert check_weak_tests.check_file(fp) == []
+
+ def test_xfail_decorated_test_not_flagged(self, tmp_path):
+ fp = _write(
+ tmp_path,
+ "import pytest\n\n"
+ "@pytest.mark.xfail(reason='not yet')\n"
+ "def test_x():\n assert True\n",
+ )
+ assert check_weak_tests.check_file(fp) == []
+
+
+class TestEdgeCases:
+ def test_non_test_function_not_flagged(self, tmp_path):
+ # Helper functions (not test_*) are out of scope.
+ fp = _write(
+ tmp_path,
+ "def helper():\n assert True\n",
+ )
+ assert check_weak_tests.check_file(fp) == []
+
+ def test_docstring_stripped_before_analysis(self, tmp_path):
+ fp = _write(
+ tmp_path,
+ 'def test_x():\n """A docstring."""\n assert True\n',
+ )
+ assert len(check_weak_tests.check_file(fp)) == 1
+
+ def test_syntax_error_returns_no_issues(self, tmp_path):
+ fp = _write(tmp_path, "def test_x(:\n assert True\n")
+ # Syntax errors are left to ruff/py_compile, not this hook.
+ assert check_weak_tests.check_file(fp) == []
+
+ def test_main_exit_code_clean(self, tmp_path):
+ fp = _write(
+ tmp_path,
+ "def test_x():\n assert compute() == 1\n",
+ )
+ assert check_weak_tests.main([fp]) == 0
+
+ def test_main_exit_code_dirty(self, tmp_path):
+ fp = _write(
+ tmp_path,
+ "def test_x():\n assert True\n",
+ )
+ assert check_weak_tests.main([fp]) == 1