What is a normal “functional lines of code” to “test lines of code” ratio?

  softwareengineering

I’m pretty new to TDD approach and my first experiments say that writing 1 line of functional code means writing about 2-3 lines of testing code. So, in case I’m going to write 1000 LOC, the whole codebase including tests is going to be something like ~3500 LOC.

Is this considered normal? What is the ratio in code you write?

13

1:3 is normal with TDD.

From my experience, and also from other citations I remember.

3

There are variations based on different coding styles and languages. However, regardless of the language you use, the biggest variation is you.

Robert Martin once said:

“As the tests get more specific, the code gets more generic.”

That made me think. More specific tests mean more test code. More generic production code means less code, so test/code ratios should rise as the code evolves.

But wait, that’s not good either. In some particular cases, for example when you define a certain algorithm, you may have only 6-10 lines of code containing a couple of “if”s, a while and maybe 2-3 recursions. I can tell you, that code will have probably more 100 lines of test code.

In a real project, something bigger than just a few algorithms, the test/code ratio should be somewhere between 1:1 and 2:1. If it gets above 2:1, it’s a smell that you have tests that should be refactored or deleted (or maybe code that is hard to test). You should always invest the same amount of care and refactoring in your tests as in your production code.

Any way, the best answer to your question maybe is “Cyclomatic Complexity”. The higher the cyclomatic complexity of your method, the exponentially more test you have to write for it to cover all the cases.

1

The ratio is going to vary depending on the size of your methods. The size of your methods will vary by programming style, language and problem domain.

If your methods are short then 3:1 is reasonable. If your methods are long then 3:1 is on the high side.

So, to answer your question, it depends. 🙂

2

For software critical application, the usual ratio is one day of testing for each 10 functional LoC.

And this is not counting TDD which is not about test but about specification.

The size of my test code is about half of what the ‘actual’ code is overall. Doing otherwise is indicative that your tests are too complex and/or your code is too hard to test and/or your code is too dense/complex.

Or you’re simply testing too much, and wasting your time on diminishing returns.

Also see “when is unit testing inappropriate or unnecessary?”

My ratio is around 2-1 to 10-1 (code to test).
Make sure you testing is about value/behaviour and not implementation.

Theme wordpress giá rẻ Theme wordpress giá rẻ Thiết kế website Kho Theme wordpress Kho Theme WP Theme WP

LEAVE A COMMENT