AI Self-Healing Tests: Automatic Broken Test Repair
Flaky tests waste time: developers investigate, realize it's UI change, fix the locator. Self-healing tests detect failures, understand root cause, and fix themselves — logging changes for review.
Self-Healing Architecture
Tests that break are automatically analyzed and repairs are suggested or applied if confidence is high.
class SelfHealingTestRunner:
async def run_with_healing(self, test_func, locators: dict):
try:
return await test_func()
except (TimeoutError, ElementNotFoundError) as e:
repair = await self.analyze_and_repair(e, locators)
if repair.confidence > 0.9:
updated_locators = self._apply_repair(locators, repair)
log_repair(test_func.__name__, repair)
return await test_func()
else:
notify_developer(repair)
raise
async def analyze_and_repair(self, error, locators) -> Repair:
prompt = f"""Test failed: {error}
Current locators: {locators}
Likely causes:
- Element moved (different selector needed)
- Element hidden (need to wait or scroll)
- DOM structure changed
Suggest repair with confidence score."""
return await self.llm.ainvoke(prompt)
Case study: 500-test suite, 15% flaky. With self-healing: 80% auto-fixed, 15% reviewed, 5% real bugs.
Timeframe: basic self-healing: 2–3 weeks.







