Writing Tests Doesn't Have to Be Extra Work
Do automated tests always have a place in your test-driven-development lifecycle? If not, is that really okay?
Join the DZone community and get the full member experience.Join For Free
writing automated tests is sort of like the kale of the software development community. with the exception of a few outlying "get off my lawn" types, there's near-universal agreement that it's "the right thing." and that leaves a few different camps of people.
the equivalent of fast food eating carnivores say, "yeah, that's something that we ought to do, but it's not for me." the equivalent of health-conscious folks plagued by cravings say "we do our best to write tests, but sometimes we just get too busy and we can't." and then, finally, the equivalent of the health nut says, "i write tests all the time and can't imagine life any other way." a lot of people in the first two camps probably don't believe this statement. i can understand that, myself, because it's hard to imagine passing up fried chicken for a kale salad in a universe where calories and cholesterol don't count.
and yet, i am that 'health nut' when it comes to automated testing. i practice test driven development (tdd) and find that it saves me time and makes me more productive on the projects i work.
it is because of that tendency that i'd like to address a common sentiment that i hear in the industry. i suspect you've heard variants of these statements before.
- "we started out writing some tests, but we got behind schedule and had to hurry, so we stopped."
- "tdd doesn't make sense in the early phase of this project because we're just prototyping."
- "we didn't want to write any tests because there's a chance that this code might get thrown out and we'd just be writing twice as much code."
the common thread here is the idea that writing the automated tests is, when the rubber meets the road, a bonus. in the world of software development, the core activity is writing the executable source code and deploying it, and anything else is, strictly speaking, expendable. you don't truly need to write documentation, generate automated tests, go through qa, update the project milestones in a gantt chart, etc. all of these things are kale in the world of food — you just need to eat any kind of food, but you'll eat kale if you're in the mood and have time to go to the grocery store, and... etc.
but if you've truly internalized tdd as not just "the right thing" but rather "the thing that makes you productive," you simply don't look at the world this way. you're now a kale eater that loves kale. you write tests first in the same way that you use your favorite ide/editor with all of your customized shortcuts and syntax highlighting arranged just so — because you're better that way. writing and leaning on your tests becomes as integral to your development as compiling the code. and in that context, re-imagine the preceding statements.
- "we started out compiling the code, but we got behind schedule and had to hurry, so we stopped."
- "compiling doesn't make sense in the early phase of this project because we're just prototyping."
- "we didn't want to compile because there's a chance that this code might get thrown out and we'd just be wasting time compiling."
that sounds absurd, clearly. but why does it sound absurd? well, it's because periodically making sure that the code builds and that you're not piling up syntax errors is just what you do. asking you not to do it would throw you off your game, and you'd be perplexed that someone would think to demand this of you.
that's how i feel when i hear such statements about automated tests and imagine applying them to my own approach. it just wouldn't work very well. and, if you're making statements like this, it's an indicator that you're in the, "i'm going to try to eat kale, but when i've had a long day, i just want a burger and a beer and for you to leave me alone" camp. you have not internalized automated testing to the point where it's indispensable to you.
and, you know what? that's okay .
getting to that point of internalization is not easy, and it requires a lot of practice. and it may even be that it's just not how you want to do things. i'm personally of the opinion that internalizing it has the potential to make you more effective but, clearly, i'm not omniscient, and i can't know that.
and if you're not at that point, then you're right when you make statements about deviating from tests when the pressure is on. if an automated test suite truly is a bonus above and beyond software delivery in your world, then it is absolutely rational to abandon it when the pressure is on and deadlines have to be met. i understand that.
but in exchange, i'd ask you to understand and acknowledge one thing: automated tests aren't a "bonus" for everyone, and they don't have to be for you.
Published at DZone with permission of Erik Dietrich, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.