Write Tests Part 1 – UnitTests

hey there,

this month topic is tests, there are many different kinds of tests and because I like to have more but smaller articles I’m going to split this article up into more then one article. As a result you will probably see my first ScreenCast I ever made, I like to make a Screencast in the last article about this topic because I want to show you a good example.

I’m going to split this articles up into:

  • UnitTests
  • How to write Tests in Java and Pharo
  • JExample
  • Write Tests First

As you can see I’m going to start with UnitTests today.

UnitTests

What are UnitTests?

A unit is the smallest piece of source code you can test and therefore a UnitTest is a method to determine if such an Unit works as intended.

In most programming languages there are tools to write tests. For example in Java you can use assertTrue, assertFalse and assertEquals to create tests or to check conditions in you’re methods (more about this later) and in Smalltalk (Pharo) you do have self assert. To go on with there are test frameworks like JUnit and SUnit which helps you organize and running you’re tests.

With the help of these methods you can start to write tests, in Java you have to create a JUnit Test Case in Eclipse and import the java.util.assert.* lib. Please notice that I’m using JUnit 4 and not JUnit 3, you should always check the used version before running a Test Case. To check this in Eclipse you right-click on the Test Case -> Run As -> Run Configurations… and in this window you can select the test runner you like.

To go on with there are only a few rules for writing UnitTests:

  • KISS (keep it short and simple)
  • At least one test per method
  • test coverage of approximately 60% or higher (more about test coverage later)
  • tests shouldn’t be longer than ten lines
  • tests should represent a scenario how the unit is used

Tests should be written during the development phase or the debugging phase. During Development you should create tests whenever you encounter one of the following problems:

  1. You need to add new functionality
  2. You need to redesign your software to add new features or adapt to new requirements

In the first case it’s very helpful to write the tests for this new functionality first and then implement the functionality, I’ll cover this in the third article. In the second case you already have a working program and you want to add new features, you therefore should refactor the old version in small, very small steps and after each of these steps you should run the tests you have again. You then have to fix everything that’s broken and when all test pass you can proceed with refactoring. As you can see tests will help you a lot during the development phase of a project but you can also use them during the debugging phase.

During Debbuging whenever you encounter a bug or a defect in your code you should write a test to demonstate that bug and as soon as this test passes you’re bug is fixed!

That’s it for this month and I hope you enjoyed the reading ^^

It’s a big goal of mine to show all of you that tests are good, a lot people I know think that tests are only a waste of time and not worth to be written. I think UnitTest will help you a lot in the future to maintain or refactor a program and you definitly get your invested time back. The best part of UnitTests is, once written, you can run them as many times as you want whitout using much time.

Please leave comments below and stay tuned for the next months article about JExample .

cheers d3orn

  1. No comments yet.

  1. No trackbacks yet.