Should you run static analysis on your tests?

I'm an advocate of both automated testing and static analysis but have mostly kept the two separate.

I've typically not run PHPStan on my automated test files - ignoring them in my PHPStan configuration.

As tests aren't production/implementation code, what's the benefit of analysing them?

Recently, though, I've challenged this and, on some projects, started to run PHPStan on my test classes.

Depending on what level PHPStan is running, the more changes you're likely to have to make to get your test classes to pass the static analysis, and whilst it results in more verbose and explicit code, I prefer that and being able to easily understand what it does rather than implicit.

If you have a bug in your test, static analysis will potentially find that, too, making your test suite more robust.

Looking at some large open-source PHP projects, they run static analysis on their test code.

Presumably, they're benefiting from it, so I'll try it and see if I get the same.

- Oliver

Was this interesting?

Sign up here and get more like this delivered straight to your inbox every day.

About me

Picture of Oliver

I'm an Acquia-certified Drupal Triple Expert with 17 years of experience, an open-source software maintainer and Drupal core contributor, public speaker, live streamer, and host of the Beyond Blocks podcast.