Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Typical size of unit tests compared to test code

I'm curious what a reasonable / typical value is for the ratio of test code to production code when people are doing TDD. Looking at one component, I have 530 lines of test code for 130 lines of production code. Another component has 1000 lines of test code for 360 lines of production code. So the unit tests are requiring roughly 3x to 5x as much code. This is for Javascript code. I don't have much tested C# code handy, but I think for another project I was looking at 2x to 3x as much test code then production code.

It would seem to me that the lower that value is, assuming the tests are sufficient, would reflect higher quality tests. Pure speculation, I just wonder what ratios other people see.

I know lines of code is an loose metric, but since I code in the same style for both test and production (same spacing format, same ammount of comments, etc) the values are comparable.

like image 432
Frank Schwieterman Avatar asked Apr 13 '10 23:04

Frank Schwieterman


People also ask

How big should unit tests be?

There is no concrete rule for the size of a unit test obviously, but there are some heuristics people use when writing unit tests. A couple of them are that a unit test shouldn't exceed a dozen or so lines and a unit test shouldn't take more than a minute to write. Unit tests are supposed to be as short as possible.

How much of the code should be unit tested?

It isn't realistic -- or necessary -- to expect 100% code coverage through unit tests. The unit tests you create depend on business needs and the application or applications' complexity. Aim for 95% or higher coverage with unit tests for new application code.

What is a reasonable code coverage for unit tests?

With that being said it is generally accepted that 80% coverage is a good goal to aim for. Trying to reach a higher coverage might turn out to be costly, while not necessary producing enough benefit. The first time you run your coverage tool you might find that you have a fairly low percentage of coverage.

What is the ideal ratio of test code to production code?

The test code / production code ratio may vary from project to project, but is ideally considered to approach a ratio of 1:1.


1 Answers

This will really depend on how well things are factored but, to my experience (yes, I did measure this on some projects), I've seen ratios from 2:1 to 5:1 (this is for tested code of course). Also have a look at the ProductionCodeVsUnitTestsRatio and UnitTestToCodeRatio pages on the C2 wiki.

like image 200
Pascal Thivent Avatar answered Nov 08 '22 09:11

Pascal Thivent