Adopting unit-testing

Jacob Proffitt has a two posts here and here on how the emphasis on TDD instead of plain unit testing is harming the adoption of unit testing.

His basic assumption (if I understood him correctly) is that people would adopt unit testing faster if unit testing proponents told them doing it without TDD is an option too.

This sounds logical if it were indeed easy to unit test without TDD. I think it's not

I found this out the hard way. Not because the testing is hard but because the code I (and some of my coworkers) used to write was highly test-resistant. I almost stopped unit testing because it took too much time to do and it took too much time to maintain the tests. The problem was that there were too much dependencies between parts of my code. Tests would be hard to write because I had to write a lot of setup code. And existing tests would fail all the time when seemingly unrelated parts of the system changed.

The solution has been well known for decades. You have to reduce coupling and increase cohesion in your code. I knew this but this is hard to maintain without immediate feedback on the quality of your code. That's where TDD comes in. By writing tests before you write code you get feedback on the testability of your code before you've written a single line of it. You're forced to think about how to call the code before it's written.

I know that I'm making an implicit assumption here too. I'm saying that testable code = high quality code. But my experience tells me this is indeed true.

If someone were to ask me how to start unit testing. I'd tell him to start using test-first on new code. Usually domain logic is easy to decouple so it's easy to test. Testing code that has been written code-first is advanced unit-testing and it's the easiest way to have people who are new to unit-testing stop doing it forever.

Print | posted on Sunday, February 24, 2008 1:56 PM

Feedback

# re: Adopting unit-testing

Left by Jacob at 2/25/2008 6:25 PM
Gravatar You don't say so explicitly, but I'm going to guess that you were writing tests at the end of projects. I agree that doing so is difficult and I wouldn't advocate doing so.

I'm more in favor of writing tests after completing a "functional unit." Doing so gives you timely enough feedback on your coupling while still allowing your design to settle a little bit before testing begins. It also allows you to see which parts of your code are trickiest and thus target with more/better tests. Indeed, I often find that a majority of my classes don't need any tests at all because their functionality is either very straight forward or better tested farther upstream. And yeah, I know that violates nine kinds of testing dogma, but it works very well in practice. Also, testing afterwards lets me enjoy all the joys of intellisense while creating my tests (without having to wrap everything in an interface first).

# re: Adopting unit-testing

Left by Mendelt Siebenga at 2/26/2008 2:10 PM
Gravatar Thank you for your reaction.
When starting unit testing I was actually trying out different things, writing tests for legacy code is definately harder than writing tests after writing functional units.
As always the truth is somewhere in the middle. When I know exactly what I'm going to write beforehand It's easier to write the tests after writing the code. When you're working out the structure of the code as you go it's easier to start test-first so get immediate feedback on the testability and quality of the code. I think people who start unit testing at least need to try out working test driven for some time.

# re: Adopting unit-testing

Left by Jeff Hardy at 3/9/2010 7:40 AM
Gravatar PortableSolarPanels-forSale.com sells portable solar panels, solar roof panels, solar powered toys and solar power guides. Also provide article about solar energy facts and solar energy blog. Solar energy facts you must know. Solar energy general facts, solar energy facts interpreting usage, facts about systems utilized for producing solar energy, interesting facts about solar energy. Solar energy pros and cons. Pros greatly outweighs the cons of solar energy. How does solar energy work. I compiled a comprehensive list of solar energy advantages and disadvantages that will enable you to make an educated decision.

# re: Adopting unit-testing

Left by Web Design London at 3/10/2010 10:41 AM
Gravatar Nice post.I like the way you start and then conclude your thoughts. Thanks for this information .I really appreciate your work, keep it up Web Design London

# re: Adopting unit-testing

Left by Miami Web Design at 3/10/2010 10:43 AM
Gravatar Adopt UnitTests first. Once people see the benefits of a simple and effective practice, they will be more willing to try out other proposed practices.

# re: Adopting unit-testing

Left by SEO UK at 3/10/2010 10:44 AM
Gravatar Teams that adopt agile practices often adopt Test Driven Development (TDD), which means, of course, that they end up writing a lot of tests. In general, thatÂ’s great but there is a failure case for teams that attempt to get test infected; you can end up writing very slow tests that take so long to run that they essentially start to feel like baggage even though they help you catch errors.

# re: Adopting unit-testing

Left by compatible ink at 3/10/2010 10:46 AM
Gravatar The agile revolution that began in software development in the 1990s has been inexorably making its way into mainstream IT organizations.

Your comment:





 
Please add 8 and 6 and type the answer here:

Copyright © Mendelt Siebenga

Design by Bartosz Brzezinski

Design by Phil Haack Based On A Design By Bartosz Brzezinski