Testing is one of the most expensive parts of software engineering. Large codebases have test suites that take hours to run, flaky tests that waste time and erode trust, and bugs that slip through anyway. It is a natural area for machine learning, because it produces huge amounts of data: every code change, test run, failure and fix.
In 2020, ML for testing and bug detection has moved from research into real engineering systems.
Predictive test selection
Running every test on every change does not scale in a large monorepo. Predictive test selection uses a model trained on historical data (which files changed, which tests failed in the past, code ownership and dependency information) to predict which tests are likely to fail for a given change, and runs those first or only those.
Facebook described its system in 2018: it catches the vast majority of individual test failures while running only a fraction of the affected tests, significantly reducing infrastructure cost and feedback time.
flowchart LR C[Code change] --> F[Features:<br/>files touched, authors,<br/>test history, dependencies] F --> M[Model predicts<br/>failure probability per test] M --> H[Run high-risk tests first] M --> L[Defer or sample<br/>low-risk tests] H --> R[Fast feedback] L --> N[Nightly full run<br/>catches the rest]
Flaky test detection
Flaky tests pass and fail without code changes, usually because of timing, ordering or environment dependencies. ML models can classify tests as likely flaky from their failure patterns and code characteristics, so teams can quarantine them rather than chasing phantom failures.
Bug prediction
Defect prediction models estimate which files or changes are most likely to contain bugs, using features such as churn, complexity, the number of authors and history of past fixes. The output helps focus code review and testing effort where it matters most.
Learning bug patterns from history
Traditional static analysis relies on rules written by experts. ML-based analysis learns patterns from millions of historical bug fixes in open-source code: if developers repeatedly changed code shaped like X into code shaped like Y, X is probably a bug.
- DeepCode, acquired by Snyk in September, uses this approach to find bugs and security issues across languages.
- Facebook's Getafix learns fix patterns from past human fixes and suggests fixes for issues found by its static analyser, Infer.
- SapFix (Facebook, 2018) went further, automatically generating and testing candidate patches for crashes found by its automated testing system, and proposing them to engineers for review.
A typical learned pattern is a missing null check that developers have fixed thousands of times:
// Pattern flagged: value from map.get() dereferenced without a check
String region = config.get("region");
return region.toUpperCase();
// Suggested fix, learned from many similar human fixes
String region = config.get("region");
return region == null ? DEFAULT_REGION : region.toUpperCase();Smarter fuzzing
Coverage-guided fuzzers such as AFL and libFuzzer, and large-scale services like Google's OSS-Fuzz, already find thousands of bugs. Research is exploring ML to generate better seed inputs and learn input grammars, helping fuzzers reach deeper code paths faster.
Limitations
- Data requirements. Most techniques need large histories of code changes, test runs and fixes, which is easier for a large tech company than a small team.
- False positives. Learned analysis can flag unusual but correct code, and developers quickly ignore noisy tools.
- Explainability. A suggestion backed by "the model thinks so" is harder to trust than a rule with a clear rationale.
- Safety nets still needed. Test selection must be backed by periodic full runs, and suggested fixes must be reviewed.
Getting started
Most teams will not build these systems themselves, but they can:
- Collect the data: keep structured records of test runs, failures, flakiness and fixes.
- Use tools that embed ML, such as learned static analysis and test impact analysis in CI platforms.
- Tackle flaky tests systematically; they undermine every other improvement.
- Measure outcomes: CI time, escaped defects and time to fix.
The bottom line
Machine learning is making testing more efficient and bug-finding more comprehensive, by learning from the enormous history of how software fails and gets fixed. It works best as an amplifier for well-engineered testing practices, not a substitute for them.
Our secure code review and application security testing services combine automated analysis with expert review, and our secure SDLC team builds these practices into client projects.