On Unit Testing

| 5 Comments


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.

5 Comments

I agree with your idea that one will feel more confident to work on the new problems if one practices unit testing and make it as part of their coding routine. Personally, I feel that I would have the urges/motivation to even work harder to fix the bugs and make all the test cases pass once I got some of them passed. Though this might not always be the case, unit testing is still an essential part to provide a better insight(what are the requirements and what kinda results are expected?) to programmers.

I agree with your idea that one will feel more confident to work on the new problems if one practices unit testing and make it as part of their coding routine. Personally, I feel that I would have the urges/motivation to even work harder to fix the bugs and make all the test cases pass once I got some of them passed. Though this might not always be the case, unit testing is still an essential part to provide a better insight(what are the requirements and what kinda results are expected?) to programmers.

Lucas Low

I really liked how you said let all your tests fail and then fix them so you can feel like you made progress. As you said these test will give you some guidance, a "to do list", and give you the feeling of progress. I know I sometimes feel lost in larger projects and it is nice to have the small successes. I also feel like I have succeeded when I make my code break, I feel like I am making my code robust when I find its flaws.

-Nicholas Ellis

The last two paragraphs in particular resonate with me. I really like the idea of writing all the tests first and then working towards making all of them pass. Also I very much agree that the benefits far outweigh the costs of unit testing.

Great job explaining unit testing. Writing out tests beforehand can be a good idea, especially if your working on a big project. When doing this, it is important to cover all test cases so that all functionality is clear to the programmer.

Ajay Jashnani

Leave a comment

About this Entry

This page contains a single entry by reis0140 published on November 8, 2011 9:30 AM.

On Subversion was the previous entry in this blog.

Find recent content on the main index or look in the archives to find all content.