Stop Filling Your Tests With Test Data

Make Your Tests More Descriptive With Less Code

Jamie Morris
Level Up Coding
Published in
4 min readSep 20, 2019

--

Photo by Markus Spiske on Unsplash

Most of the developers I talk to agree that tests are valuable, that they ought to be writing more tests. Even the developers who aren’t writing tests rarely argue with the value in doing so. But occasionally I talk to someone that dislikes tests. Often the complaint I hear is something like this:

“But for every 10 lines of code I write, I need 20 lines of tests!”

Get used to it, because if you’re writing more code in your features than in your tests, you probably aren’t writing enough tests.

And I don’t mean test data either— I mean setup and assertions. Your test data should be short and clear. Unfortunately, this is rarely the case, and more often than not, I see manually curated test data at the top of a spec file. Here’s a fairly contrived example:

Of the 54 lines in this very brief spec file, 38 lines are test data. Now, it could be argued that the problem is much more severe with a single test, and that is true — if I wrote more…

--

--

I used to be a full stack developer, but nowadays most people only want me for my <body>, so I try to be a UI expert. All opinons are my own, not my employer's.