Archive for June, 2009

Unit tests and Use Cases

June 14, 2009

A common challenge when writing unit tests for a large code base is keeping track of what you are testing. Without a good process for categorizing what the individual tests are actually testing, it can quickly become unmanageable. This is especially true for projects with several hundred tests.

The way I have decided to tackle this challenge in ExpressUnit is through use cases. For those of you unfamiliar with the concept of use cases, here’s the Wikipedia definition:

“A use case in software engineering and systems engineering is a description of a system’s behavior as it responds to a request that originates from outside of that system. In other words, a use case describes “who” can do “what” with the system in question. The use case technique is used to capture a system’s behavioral requirements by detailing scenario-driven threads through the functional requirements.”

Source: http://en.wikipedia.org/wiki/Use_case

Since use cases are a description of how the system should behave, I find them to be a very natural grouping for unit/integration tests. The mechanism for grouping tests by use cases in ExpressUnit is actually quite simple. I have simply extended the test attributes (UnitTest/IntegrationTest) to take in an optional use case parameter. The new syntax works as follows:

 [UnitTest(UseCase="Confirm.Equals will return true if objects are equal")]

public void ConfirmDoubleObjectsAreEqualTests()

{

    Confirm.Equals(1.0, 1.0);

}

The above example is from ExpressUnit’s own Confirm test suite where all Confirm.Equals tests have been linked to the use case: “Confirm.Equals will return true if objects are equal”.

Version 3.2 of ExpressUnit contains a new UI window for displaying test results broken down by use cases. (See Figure1). You can navigate to this window by clicking the Use Case tab after running all your tests.

Figure1

Figure1

Continuous integration: ExpressUnit and Hudson

June 6, 2009

To manage large software projects with many developers and a large code base, it’s very useful to automate builds and test runs through continuous integration. The most common setup is to have a continuous integration server build your source code and run all your tests on code check-in. This gives you almost immediate feedback on whether or not your latest code check-in broke the build by either causing a compiler error or test failure. There are several alternatives out there for continuous integration. I have chosen to integrate ExpressUnit with Hudson which is an open source continuous integration platform. The nice thing about Hudson is that it’s very easy to configure. The interface is extremely user friendly and lets you handle everything through a simple web based UI. I won’t go into much detail on how to set up Hudson as there are plenty of good tutorials available on the web. Instead I will list some of the necessary steps to integrate ExpressUnit with Hudson

1)Add your source code management tool through the Hudson web UI

2) Set up MsBuild as your source code builder (point to your solution file)

3) In the Windows batch command window make a call to “ExpressUnitGui.exe runtests” which triggers ExpressUnit to run and close after all tests have been run (No user interaction required)

4)Select to publish JUnit test result report. ExpressUnit will, when running in command line mode, generate an xml file with test results which will be picked up by Hudson and displayed in the UI. The generated xml file is compliant with the JUnit test result format required by Hudson, so Hudson will be able to display information about your test runs. You will be able to see statistics of test runs over time as well as information about why particular tests failed.

I am actually using ExpressUnit as my testing framework while building ExpressUnit. The following screen shot shows my ExpressUnit project on Hudson

Figure1

Figure1

On the left you can see the build history, and over on the right is the test trend graphed over time.

Please drop me a line if you need more detailed instructions on how to get ExpressUnit up and running with Hudson