4

I'm fairly new to unit testing. In school it's always been, "hey it works, onward!" But I've started to write professionally, and even at work that's been basically the mantra. However, I've started to see the validity of unit testing and TDD. I've come to a realization, that when I write maybe a 20 line piece of code, I'll usually write a 100-250 lines of code testing those lines of code. Is this about average? Are there better best practices of unit testing that I'm not aware of?

Any way, I thought it was an interesting observation and was wondering, on average how much more code do you write when you write your unit tests?

user133688
  • 175
  • 4

2 Answers2

4

For SQL Lite, testing level is high ... and I mean it!

http://www.sqlite.org/testing.html

As of version 3.8.0, the SQLite library consists of approximately 84.3 KSLOC of C code. (KSLOC means thousands of "Source Lines Of Code" or, in other words, lines of code excluding blank lines and comments.) By comparison, the project has 1084 times as much test code and test scripts - 91452.5 KSLOC.

3

In general, I put the line at "unit tests should be about half the size of the code they test". Occasionally it will be more for vital/complex code, but more often than not it just means I'm testing too much. That is, I'm writing a bunch of tests that are unlikely to find or prevent bugs.

I don't do much in python, and I don't know how you're writing tests, so I can't say if you're doing something poorly/inefficiently. It is possible that you're missing a good mocking framework. More likely, you just have hard to test code (perhaps because of what it's doing, perhaps because it's poorly designed) and are testing way too much.

Telastyn
  • 110,259