|
| 1 | +"""Cluster tests that flake *together* by co-failure similarity. |
| 2 | +
|
| 3 | +Flaky tests are rarely independent: a wobbly shared fixture, a slow dependency or |
| 4 | +a noisy environment makes a *group* of tests fail in the same runs (research finds |
| 5 | +~75% of flaky tests fall into co-failure clusters). Ranking tests one-by-one by |
| 6 | +flip rate misses that shared root cause. ``flake_cluster`` measures how often each |
| 7 | +pair of tests fails in the *same* runs (Jaccard similarity over the set of runs |
| 8 | +each failed in) and groups tests whose co-failure exceeds a threshold into |
| 9 | +clusters — so you can chase one root cause instead of N symptoms. |
| 10 | +
|
| 11 | +Input is a list of runs, each a collection of the test names that failed in that |
| 12 | +run. Pure standard library; no device, no ``PySide6``. |
| 13 | +""" |
| 14 | +from itertools import combinations |
| 15 | +from typing import Any, Dict, List, Sequence, Set |
| 16 | + |
| 17 | + |
| 18 | +def _set_jaccard(left: Set[int], right: Set[int]) -> float: |
| 19 | + union = left | right |
| 20 | + return len(left & right) / len(union) if union else 0.0 |
| 21 | + |
| 22 | + |
| 23 | +def _fail_runs(runs: Sequence[Sequence[str]]) -> Dict[str, Set[int]]: |
| 24 | + """Map each test name to the set of run indices in which it failed.""" |
| 25 | + fails: Dict[str, Set[int]] = {} |
| 26 | + for index, run in enumerate(runs): |
| 27 | + for test in set(run): |
| 28 | + fails.setdefault(str(test), set()).add(index) |
| 29 | + return fails |
| 30 | + |
| 31 | + |
| 32 | +def cofailure_pairs(runs: Sequence[Sequence[str]], *, |
| 33 | + threshold: float = 0.5) -> List[Dict[str, Any]]: |
| 34 | + """Return test pairs whose co-failure Jaccard meets ``threshold``. |
| 35 | +
|
| 36 | + Each entry is ``{tests:[a,b], jaccard, co_failures}``, most similar first. |
| 37 | + """ |
| 38 | + fails = _fail_runs(runs) |
| 39 | + pairs = [] |
| 40 | + for left, right in combinations(sorted(fails), 2): |
| 41 | + score = _set_jaccard(fails[left], fails[right]) |
| 42 | + if score >= float(threshold): |
| 43 | + pairs.append({"tests": [left, right], "jaccard": round(score, 3), |
| 44 | + "co_failures": len(fails[left] & fails[right])}) |
| 45 | + pairs.sort(key=lambda pair: pair["jaccard"], reverse=True) |
| 46 | + return pairs |
| 47 | + |
| 48 | + |
| 49 | +def _connected_components(nodes: Sequence[str], |
| 50 | + adjacency: Dict[str, Set[str]]) -> List[List[str]]: |
| 51 | + seen: Set[str] = set() |
| 52 | + components = [] |
| 53 | + for node in nodes: |
| 54 | + if node in seen: |
| 55 | + continue |
| 56 | + stack, component = [node], [] |
| 57 | + while stack: |
| 58 | + current = stack.pop() |
| 59 | + if current in seen: |
| 60 | + continue |
| 61 | + seen.add(current) |
| 62 | + component.append(current) |
| 63 | + stack.extend(adjacency[current] - seen) |
| 64 | + components.append(component) |
| 65 | + return components |
| 66 | + |
| 67 | + |
| 68 | +def _cohesion(component: Sequence[str], fails: Dict[str, Set[int]]) -> float: |
| 69 | + scores = [_set_jaccard(fails[a], fails[b]) |
| 70 | + for a, b in combinations(component, 2)] |
| 71 | + return round(sum(scores) / len(scores), 3) if scores else 1.0 |
| 72 | + |
| 73 | + |
| 74 | +def failure_clusters(runs: Sequence[Sequence[str]], *, threshold: float = 0.5, |
| 75 | + min_size: int = 2) -> List[Dict[str, Any]]: |
| 76 | + """Group tests that fail together into co-failure clusters. |
| 77 | +
|
| 78 | + Builds a graph linking test pairs whose co-failure Jaccard meets |
| 79 | + ``threshold``, then returns its connected components of at least ``min_size`` |
| 80 | + tests as ``[{tests, size, cohesion}]`` (largest / most cohesive first). |
| 81 | + ``cohesion`` is the mean pairwise Jaccard within the cluster. |
| 82 | + """ |
| 83 | + fails = _fail_runs(runs) |
| 84 | + tests = sorted(fails) |
| 85 | + adjacency: Dict[str, Set[str]] = {test: set() for test in tests} |
| 86 | + for left, right in combinations(tests, 2): |
| 87 | + if _set_jaccard(fails[left], fails[right]) >= float(threshold): |
| 88 | + adjacency[left].add(right) |
| 89 | + adjacency[right].add(left) |
| 90 | + clusters = [] |
| 91 | + for component in _connected_components(tests, adjacency): |
| 92 | + if len(component) >= int(min_size): |
| 93 | + clusters.append({"tests": sorted(component), "size": len(component), |
| 94 | + "cohesion": _cohesion(component, fails)}) |
| 95 | + clusters.sort(key=lambda cluster: (cluster["size"], cluster["cohesion"]), |
| 96 | + reverse=True) |
| 97 | + return clusters |
0 commit comments