7 Tools for Analyzing Test Code with Static Analysis
Seven tools for static analysis for test code, from ESLint and Pylint to mypy, Semgrep, and Bandit, and what each one catches before tests run.
On this page
Key takeaways
- Static analysis checks test code without running it, so it catches mistakes early and cheaply.
- ESLint and Pylint flag style and correctness problems, while mypy and Pyright add type checking.
- Semgrep lets teams write custom patterns, PHPStan covers PHP, and Bandit adds a security lens.
- Linters and type checkers work together, and both can be tuned per test directory.
Quick comparison
| Tool | Best for | Starting price | Free plan |
|---|---|---|---|
| ESLint | Linting JavaScript and TypeScript test files | Not included | Not included |
| Pylint | Catching errors and smells in Python tests | Not included | Not included |
| mypy | Type checking Python test code | Not included | Not included |
| Pyright | Fast type checking in Python projects | Not included | Not included |
| Semgrep | Custom rules across many languages | Not included | Not included |
| PHPStan | Static analysis of PHP test code | Not included | Not included |
| Bandit | Finding security issues in Python code | Not included | Not included |
Static analysis tools that keep test code clean
1. ESLint
- Official site: https://eslint.org
- Best for: Linting JavaScript and TypeScript test files
- Starting price: Not included
- Free plan: Not included
- Pros: Huge plugin ecosystem; configurable rules per file type
- Cons: Config can be complex; false positives need tuning
ESLint is a linter for JavaScript and TypeScript that scans test files for style and correctness problems before they run. With plugins for Jest and Vitest, it can flag broken assertions, missing awaits, and test files that skip cases or leave focused tests in place.
Rules apply per file and glob, so test code can follow a stricter or looser policy than application code. ESLint runs fast in editors and CI, making it a natural first step for static analysis for test code in any JavaScript project.
2. Pylint
- Official site: https://pylint.readthedocs.io/en/stable/
- Best for: Catching errors and smells in Python tests
- Starting price: Not included
- Free plan: Not included
- Pros: Many built-in checks; per-file config
- Cons: Noisy without tuning; slower on large codebases
Pylint is a static analyzer for Python that reports errors, style issues, and code smells. On test files it can flag undefined names, unused imports, and patterns that hide bugs, such as broad exception handling or duplicate test logic.
It supports per-file configuration, so test code can disable rules that do not apply, like docstring requirements on test functions. Pylint is a dependable way to add static analysis for test code in Python projects that want a thorough, opinionated checker.
3. mypy
- Official site: https://mypy-lang.org/
- Best for: Type checking Python test code
- Starting price: Not included
- Free plan: Not included
- Pros: Catches type errors without running tests; strong incremental mode
- Cons: Needs type annotations to help; can be slow on first run
mypy is a type checker for Python that reads type annotations and reports mismatches before the code runs. Applied to tests, it catches wrong argument types, missing attributes, and incorrect fixtures that would otherwise fail only at runtime.
Type checking test code also keeps mocks and fakes honest, since a fake that does not match the real object's interface will be flagged. mypy integrates with editors and CI, and its incremental cache keeps repeat runs quick.
4. Pyright
- Official site: https://github.com/microsoft/pyright
- Best for: Fast type checking in Python projects
- Starting price: Not included
- Free plan: Not included
- Pros: Very fast; strong editor integration
- Cons: Fewer plugin options; stricter defaults can surprise
Pyright is a static type checker for Python known for its speed and editor support. It checks type usage across a project, including test files, and reports issues like type mismatches and unknown attributes without executing anything.
Because it is fast, Pyright runs on every save in the editor and in CI without slowing the pipeline. For teams that want quick static analysis for test code with good editor tooling, Pyright is a strong modern option.
5. Semgrep
- Official site: https://semgrep.dev
- Best for: Custom rules across many languages
- Starting price: Not included
- Free plan: Not included
- Pros: Write rules as patterns; many community rules
- Cons: Pattern writing takes practice; some languages have limited coverage
Semgrep is a static analysis tool that matches code patterns across many languages. Teams write rules that look like code, such as a pattern for a test that disables error reporting, and Semgrep finds every similar spot in the test suite.
It ships with a large registry of community rules and can enforce project-specific conventions, like forbidding sleeps in tests or requiring a helper for common assertions. That flexibility makes Semgrep a useful layer of static analysis for test code in multi-language repos.
6. PHPStan
- Official site: https://phpstan.org
- Best for: Static analysis of PHP test code
- Starting price: Not included
- Free plan: Not included
- Pros: Deep type analysis; strict levels from loose to strict
- Cons: Requires annotations for full value; config takes initial effort
PHPStan is a static analyzer for PHP that tracks types through a codebase and finds errors in test files, such as calls to methods that do not exist or arguments with the wrong type. Its levels let teams start loose and tighten the rules over time.
It also understands popular testing frameworks, so it can flag incorrect assertions and mock usage. For PHP projects, PHPStan brings the same depth to test code that it brings to application code, catching mistakes before the suite runs.
7. Bandit
- Official site: https://bandit.readthedocs.io
- Best for: Finding security issues in Python code
- Starting price: Not included
- Free plan: Not included
- Pros: Focused on security patterns; easy to run in CI
- Cons: Limited to Python; needs triage for false positives
Bandit is a security-focused static analyzer for Python that scans code, including tests, for dangerous patterns like shell injection, unsafe deserialization, and hardcoded secrets. It reports each finding with a severity level so teams can triage what matters.
Running Bandit on tests is useful because test fixtures and setup often contain example credentials or file paths that should never reach production. It slots into CI as a simple command, adding a security check to static analysis for test code.
How we chose
We focused on tools that analyze test code without executing it, across the languages teams commonly test in. ESLint, Pylint, and PHPStan catch correctness and style problems, mypy and Pyright add type checking, Semgrep brings custom patterns, and Bandit adds a security lens.
Each tool is actively maintained and widely adopted. The right mix depends on your language and how strict you want to be, and many teams run a linter plus a type checker together for overlapping coverage.
FAQ
What does static analysis catch in test code?
It finds problems visible without running the suite, such as undefined names, wrong argument types, unused imports, and risky patterns like disabled assertions or hardcoded secrets. Some tools also enforce project-specific style rules on tests.
Is static analysis a replacement for running tests?
No. Static analysis finds structural and type errors, but it cannot prove that behavior is correct. It should run before or alongside tests to catch cheap mistakes early and let the suite focus on actual behavior.
Do I need both a linter and a type checker?
Often yes. A linter flags style and smell problems, while a type checker verifies that types line up. They overlap a little but catch different classes of bugs, so combining them gives broader coverage of test code.
How do I stop static analysis from being noisy on test files?
Configure separate rules for test paths, since tests legitimately use patterns like broad imports or short helper names. Most tools support per-file or per-glob settings so test code can relax rules that do not apply to it.
Can static analysis tools be customized?
Yes. Tools like Semgrep and ESLint let you write your own rules, and Pylint and PHPStan expose many options and severity levels. Start with defaults, then add rules that match the mistakes your team actually makes.