logo
podcast Podcast
get help Get Unstuck

Why would I leave an unused variable in my test?

It wasn't a mistake

Joel Clermont
Joel Clermont
2025-07-09

Earlier this month, I shared a tip about how I fine-tuned Rector's dead code detection to allow our project to have unused variables, but only in the tests folder.

It generated a question that I thought would be worth sharing here. Why didn't I use a comment for documentation, instead of relying on an unused variable?

All of the records are important to the code being tested. Some should match my search logic and some should not match.

Because of that, I assign each to a variable that I can use for assertions later in the test.

That being said, there is a technique where I pair a count assertion with the specific records I expect to be returned. By combining those two things, it becomes redundant to also assert the absence of the other records. I leave my variables there for consistency and readability.

Here to help,

Joel

P.S. Were you reading these nuanced points on test assertions and thinking, "I just wish my project had any tests at all"? If so, let's see how I can help.

Toss a coin in the jar if you found this helpful.
Want a tip like this in your inbox every weekday? Sign up below 👇🏼
email
No spam. Only real-world advice you can use.