10 Reasons to Write Unit Tests

 

** Write your first unit test in 8 easy-to-follow steps! First step is: watch this webinar: 7 steps to writing your first unit test **

I came across this great list of 10 reasons to write unit tests. It’s an old post, but still very good points. Here are one person’s thoughts:

  1. Don’t let your customers discover embarrassing bugs. Write tests to cover a multitude of scenarios and catch these bugs before they get into production.
  2. Test complex scenarios quickly, without having to manually reproduce them in the application.
  3. By testing often, you don’t break the application as you go. You can’t always know the indirect implications of what you’re writing, especially if you didn’t write the original application.
  4. By testing early, you don’t write unnecessary code but only the strict necessary. This makes the codebase smaller and more maintainable. It also saves on development time.
  5. You do not have to debug the same code twice. Once you have a test to account for a possible bug, you’ll pick up any wrong turns quickly.
  6. You ensure readability. A unit test makes the purpose of your code easier to understand.
  7. You ensure maintainability. Unit-testing forces you to better encapsulate functionality, thus making it easier to maintain and add new features.
  8. Refactor without worries. Run the tests to make sure everything still functions as intended.
  9. Save time on testing. You can test the entire application at the speed of your CPU.
  10. Feel safer. How many times were you afraid to add a new feature or change something in your application’s core? No more!
  11. Bonus: know exactly what is broken. Instead of hunting for an obscure bug, let the tests tell you what’s wrong and why. Example: the application will tell you when you add an item to a cart but the cart still appears empty. It will also tell you what item you tried to add for the cart to break.

What are yours? (And, of course, don’t forget to comment on Anna’s blog as well).

Inspired? Want to learn to unit test? Watch our on-demand webinar: ”Intro to Unit Testing, where we explain how to start unit testing successfully for better code and fewer bugs.

  • Pingback: Developer Forums » 10 Reasons to Write Unit Tests » Developer Forums

  • Nicolas

    All the arguments are nice but Unit Tests alone don’t provide this. They just participate to provide this together with integration & validation tests, formal code reviews, beta testing, stress tests…

    In particular a Unit Test only test what the developper think the code should do, and for the type of usage the developper expect. A unit test fail to show integration or concurrency problems, wrong design, wrong usage of external libraries. It typically doesn’t show neither performance or scalability problems.

    Unit test together with code coverage can make somebody very confident about their code while the code still contain many bugs. Trusting them to much can make the developper perform a modification and introduce regression… because the JUnit bar is still green !

    On the opposite, they can consume lot of time to check hedge case (like some rare exception handling case), while nominal case doesn’t work as intended.

    Like everything in life, the key is to use them wisely.

  • http://www.itoctopus.com itoctopus

    As Nicolas was saying, Unit Tests are just tests to see if there are no technical bugs, but logical bugs are not handled by them. Some would say that unit testing is overhead in small projects.

  • Jim

    Copying someone else’s blog post word for word is still bad form even when you add a link to the original source you know.

  • Bob

    Unit testing provides a sort of living documentation of the system

TOP