Unit test checkboxesIn my third of three related postings on some core programming principles, I’ll be talking about unit testing.  In the first part, we discussed code reuse, and in the second, I went over version control.

Unit testing is a methodology by which you run tests on individual bits of your programming code.  I have to admit that this is the topic of the three with which I am the least familiar.  The concept of “testing” in my past jobs has largely been comprised of this:

  • Write your program
  • Test it out as thoroughly as you can when done
  • Turn it over to a user for them to test thoroughly.

This is actually a very broad generalization, and I would, in fact, test out bits of code that I wrote as I wrote them, making sure that functions returned the desired results before I was anywhere near to having a final product.  While this is a step in the right direction towards unit testing, it is no means formal unit testing.

Ideally, you would want an easily replicable suite of unit tests that you can run at any point in the lifetime of your project to see that the various parts of your code work and return the desired results. There are many tools available now that can automate your unit testing; after making a change to your code base, you can run your unit test tool and see if you accidentally broke anything.

The benefits of this should be obvious – you can find errors in your code MUCH sooner than by completing your program and then testing it out.  Unit testing involves a lot of additional work beyond just writing a program (you need to keep your tests up to date, also), but the end result should be that you are much more confident in your program being error-free.

An extension of this concept is test-driven development.  You design your tests around a theoretical programming unit first, and then you write the program unit to pass your tests.  This is an area into which I’ve been doing some research, and I plan to try out this programming technique in my next major project.  Just like with version control, I’m eager to hear others’ experiences with unit testing so that I can learn from you, too!

To conclude this brief series on some very important programming principles, I’ll mention that studying and learning new programming techniques is a very important methodology, as well.  No one starts their programming career knowing every best practice there is to know, and keeping abreast of the latest programming methodologies is one of the best ways to ensure your success in the field.

Tagged with:
 

Leave a Reply