Unit testing is a very important tool that can and should be used when working on software projects. Unit tests can help you define interfaces by writing the tests for those interfaces first. If you write the tests first you have a clearer purpose for the module you are coding. This often allows you to find design flaws before you implement them saving more time than it took to write the tests.
Once you have written a new interface and have passed the tests you can now use the test as a regression test to run every time new code is checked in or modified to ensure your class is still working properly. The more tests you have the more execution paths you can cover with your tests and the more confidence you can have that your code stills functions as before.
A test should be short and test one single case. The more details you cover with your tests the easier it will be to pinpoint bugs in your code. The tests also become documentation as they detail how each function is supposed to work. So when another programmer wants to use your module they can look at your tests for code examples of its functionality.
A test suite is a collection of tests that will be run in succession. One test suite should cover the functionality of an object. Test suites are a way to organize you tests, each suite should stay with the code it tests to be used when it is integrated into other projects.
When you want to test the result of method call you can write an assert statement to compare the result to the expected value. If the result is not as expected the test has failed and a message will be displayed. If the test passes no message will be printed. No news is good news I guess.
A setup functions can be defined in your test suite to be called before each test is run to create a new object so you start your test with know object state. You can also create a setup function for the test suite that will be run only once in the test suite. You can setup global resources in this function, such as database connections.
A teardown is the similar to a setup function except it is called after your test is run to clean up the resources created for the test. Likewise a teardown function can be created for the test suite to handle global clean up.
Write the tests first, let them all fail and then complete your code making each test succeed. This gives you a feeling of progress that can sometimes be lost in big projects. Also the tests provide with a to do list of functionality. Once all your tests pass you know you are done with that class, at least until you find a bug. Then you can write a new test to expose the bug and squash it.
Unit testing offers many benefits which often outweigh the cost in time of writing them. Once it becomes part of your development style you should have more confidence to tackle new problems. This confidence is also a feels really good that you write code that works and you have something to show for it. Everyone should try to make unit testing part of their routine.
