I'll admit it, when I'm writing code just for myself to learn stuff I tend not to unit test. When I'm in a team environment I insist on it as the person coding isn't going to be the person maintaining. Unit Testing has lots of benefits in terms of project success, code quality and longevity. If you think testing first then you tend to think about the solution to the problem a bit more and that is always a good thing. I tend to think about design by contract over unit testing, but unit testing tends to be the only way to build such things.
So lets take an example... file compression. The sort of thing that a single class can easily do. We've got two functions
char *compress(char *bytes)
char *uncompress(char *bytes)
Right now what are our core unit tests?
Well the first one of course is that if we compress something then uncompress it then it must be the same as the original. The second one is also pretty obvious namely that the compress method should result in something smaller than the input. Pretty much the basic method for such things.
Over at Microsoft Office however the 'compress' function built into MS Office, at least on the Mac, which is a one time thing to 'reduce file size' has a slightly different approach. When you do 'reduce file size' on a presentation it manages to sometimes increase the file size.
That sort of thing should really have been caught in Unit Test.
So lets take an example... file compression. The sort of thing that a single class can easily do. We've got two functions
char *compress(char *bytes)
char *uncompress(char *bytes)
Right now what are our core unit tests?
Well the first one of course is that if we compress something then uncompress it then it must be the same as the original. The second one is also pretty obvious namely that the compress method should result in something smaller than the input. Pretty much the basic method for such things.
Over at Microsoft Office however the 'compress' function built into MS Office, at least on the Mac, which is a one time thing to 'reduce file size' has a slightly different approach. When you do 'reduce file size' on a presentation it manages to sometimes increase the file size.
That sort of thing should really have been caught in Unit Test.