How do you do Real-world Unit Testing?

3 points by toxik ↗ HN
I often hear about the benefits of unit-testing and how great it is to have a complete test suite and so on, but I've many times found it way too cumbersome to actually make unit-tests for the code I write.

For simple functions that only operate on scalables and return scalables, unit-testing is easy. However, it becomes a wildly different story when things like database connections, configuration files, memcached servers etc. are in play.

How do you test something like an IRC connection? Obviously you could set up a phony socket which doesn't really use the network, but that would in some cases defeat part of the test.

Dear YC, how do I make unit-tests with auxiliary resources?

2 comments

[ 31.8 ms ] story [ 351 ms ] thread
I think testing things like connections, caches, etc are outside the scope of unit testing. What you probably want to unit test is how your code handles a malformed NICK command in an IRC connection, or random input, not the entire connection as a whole.

You write unit tests for these handlers and when that's done, you can write scripts that are grander in scope and nature. They can build and tear down connections, or perhaps drop it in semi-random intervals. As far as I know, unit tests are made to test fairly small-scale blocks of code and bigger frameworks and scripts can test whole subsystems.

Difficulty Unit Testing often reveals deficiencies in design. Is your system divided into largely orthogonal parts?