November 2011 Archives

On Unit Testing


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.

On Subversion


There are many flavors of version control to available to programmers. Subversion is one of them and quite popular due to its ease of use and flexibility. I have used subversion for a couple of months and have found it handles core version control functionality in a very straight forward manner. There are a small number of commands to learn to get started.

Using subversion, sharing with my partner was a breeze. Armed with a URL, user name and password, one simple 'svn checkout ' command was all I needed to get started. Then I could add files to the repository or modify and upload any existing files. My partner could also download and modify the files I had added, or add his own. This allowed us to split the project work between us easily. As long as we would commit our changes and update changes from the server frequently, we were essentially working in the same file system.

Working with a partner there were times when we modified the same file. Subversion automatically merged the changes, as long as they were located in different parts of the file. If the same part of the file had been modified, it would mark those lines in the file. That made it easy to see the changes we each made and decide which code to keep.

Subversion also keeps a history of everything you commit. So if you ever need to get back to a point where things may have worked a little better you have that option. You can also create a copy of a source directory to have a snapshot of a software release, also know as a tag. This comes in handy once you have a program working but want to refactor your code to be more readable and modularized. You do not have to worry about losing that functionality since the code is with the repository and you can always revert.

Here is a quick primer on the basic commands of subversion. If a repository has been created on a server, all you need is the URL, user name and password. Then use the 'svn checkout ' command to create a local copy. You will be prompted for the user name and password. The a copy of the repository files will be downloaded from the server into your local file system.

After you have a copy on your local machine you can use 'svn add ' to add files and 'svn commit ' to upload any changes to the server. When you want to see what files you have modified or created but not added, you can use 'svn status' to list them. You may also see in the status that files have been modified remotely by another user. You can then issue the 'svn update' command to download and merge those changes with your local copy. If you wish to ditch your current changes to a file, the 'svn revert ' command will re-sync your local copy with the server.

Overall subversion is a simple a powerful version control tool. Using subversion can save you many work hours with its history backup. It also eases the pain of transferring files between computers. These features are useful if you are working in a group or alone. Also there are programs available that integrate the features of subversion into your file system manager. This makes using subversion as easy as a right click.

About this Archive

This page is an archive of entries from November 2011 listed from newest to oldest.

October 2011 is the previous archive.

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