Tuesday, March 25, 2008

10 Principles for Developer Testing

1) Code a Little, Test a Little

Test driven development is all the rage and is a great way to do "test a little, code a little". But it is important to remember that it is the results not how you achieve them that matter most. As long as test code is written concurrently with implementation code, it doesn't matter if you follow strict test driven development practices because when the tests are done you won't be able to tell what method was followed. The only wrong way to write test code is after the implementation code is done.

2) Use Tests to Improve the Design

Testability is an excellent indicator of design quality. If your code is hard to test then the design needs to be fixed. Look for warning signs and use the tests to guide the design. Hard to test code includes singletons, static calls, and too many parameters. Warnings signs of a design that needs help include making private methods public for testing, complicated test setup, excessive test assertions and mocking concrete classes.

3) Write as Much Test Code as Implementation Code

On average you should expect to write at least as many lines of test code as there are implementation code.

4) Test the Right Thing

It's a unit test, so it should be testing one unit. Since most of us are doing object oriented development the unit is a class. Make sure you are only testing the class under test and not some infrastructure such as database or web server. Make sure you are only testing the class under test and not other classes it may depend on, the dependent classes should already have been well tested by their own unit tests. Make sure you test the things that are likely to break and that usually does not include things like simple getters and setters.

5) Test the Error Handling

You need to test more than the expected paths through the code. Make sure to test what the code will do under error conditions. Is it catching the right exceptions? Does it handle errors gracefully and cleanup things like database connections?

6) Always Write a Test Before Fixing a Bug

Whenever you find a bug write a test that fails because of the bug before you try and fix the bug. If you don't have a test before you attempt fix the bug how exactly will you know the bug is gone? Adding a test for every bug will also help ensure that the same bug does not creep back into the system later.

7) Mock Objects are Your Friends

If you are not using mock objects you need to start. There are numerous excellent and mature open source mock object frameworks for most any language. The benefits of mock objects are to great to ignore. Mock objects let you write truly isolated unit tests, speed up you unit tests, and test things such as error handling that would probably be impossible to automate without mocks.

8) Use a Code Coverage Analyzer

Test coverage analyzers are wonderful tools that can help find inadequately tested code. But always keep in mind that test coverage is about the journey not the destination. Your goal is to have better tested code not necessary 100% code coverage. There are many excellent open source and commercial coverage tools available so there is no reason not to use one.

9) Don't be Fooled by High Code Coverage Numbers

Depending on the metric used it is possible to have 100% coverage with garbage unit tests. Statement coverage is the poorest way to measure coverage because it only says which lines were executed and says nothing about what branches or paths in the code were tested. Branch coverage tells you which branches in a control structure such as an if or switch statement were executed. So branch coverage combined with statement coverage is pretty good. A better metric is path coverage that will tell you which execution paths through the code were covered.

10) Know Your Cyclometric Complexity

Cyclometric complexity is a measure of how many execution paths there are through a piece of code. A method or function with no control flow, such as a if statement, will have a complexity of one. The complexity goes up for each execution branch such as and if or else statement. Cyclometric complexity is a good way to get a feel for how many tests a method needs to cover all execution paths through it.

6 comments:

Joshua Smith said...

Thanks for your help in describing this. Great review! Also turn your attention that it needs to search for mobile software development companies if you need mobile applications development.

Joshua Smith said...

Thanks for nice article. Let me mention about homeowners insurance for USA policy owners house insurance providers. Compare free online rates on homeowners insurance.

segundo whatsapp said...

Gran blog, me encanta. Lo compartiré con mis amigos.
instalar dos Wasap en Android
segundo Wasap en Android
dos Wasap en el mismo teléfono
dos Whatsapp en el mismo teléfono
dos cuentas de Wasap en el mismo móvil
dos cuentas de Whatsapp en el mismo móvil
frases y estados para wasap
frases y estados whatsapp
frases y estados graciosos para wasap
frases y estados de amor para wasap
frases y estados de peliculas para wasap
frases y estados de canciones para wasap
imagenes para wasap
imagenes para whatsapp
imagenes de amor para wasap
imagenes graciosas para wasap
imagenes reflexivas para wasap
wasap plus
whatsapp plus

instalar play store said...

Me ha resultado muy beneficioso leer este artículo. Gracias.
instalar google play
descargar play store gratis
play store para pc
instalar play store en móvil
instalar play store en tablet

anjali said...

python shuffle list
convert list to numpy array
header fixed on scroll jquery
scrape content from website php
args and kwargs in python
python program to print hollow diamond pattern
sum of list in python

شركة اوائل الخليج said...

نقل الأثاث الثقيل ليس بالأمر السهل على الإطلاق ، وحتى أقل من ذلك عندما يتعين عليك تحريكه لأعلى الدرج! سواء كنت تنتقل إلى منزل جديد في طابق أعلى في شقة لا تحتوي على مصعد أو تحتاج إلى نقل بعض الأثاث إلى طابق آخر في المنزل ، فمن المهم استخدام الأسلوب الصحيح للقيام بذلك بأمان. اتخذ بعض الإجراءات لحماية الأثاث ومنزلك ونفسك من التلف والإصابة قبل أن تبدأ في نقل أي شيء. احمل قطعًا كبيرة على الدرج باستخدام مساعد أو استخدم عربة لنقل القطع الثقيلة الصغيرة والمتوسطة الحجم بمفردك بينما يكتشفك شخص ما من الأسفل. استفد من المعدات والتقنيات المتخصصة لتسهيل رفع بعض العناصر ووضعها في مكانها.
تقديم خدمة نقل الأثاث بأفضل خدمة وأقل تكلفة وأسرع وقت وملائم للعملاء لتنفيذ عملية نقل الأثاث وتوفير المال والوقت للعملاء. جميع خدمات شركتنا بمعنى الخدمات الشاملة.

شركة نقل عفش