October 20, 2007

 Software Development Essential Practices

I was in an interesting discussion last night: what software development practices do you consider essential? 

While opinions varied, I think there was some consensus on a few items.  In my list I also include some tools.  Some of these were mentioned by the group, but consider them my own additions.  Also, I am making this list specific to .NET, but the list applies to just about every language.

Universally agreed Practices

  • Source Control (1)
  • Local Development -- Every developer should have everything on their computer to run the system.  This includes all of the software and all of the source code.
  • Bug Tracking - nuf said.  There are to many tools available to list here.
  • TDD - Test Driven Development.  There was a little bit of back and forth, but the general consensus was that this should be done.
  • ORM - Object-Relational Mapping
    • NHibernate 
    • SubSonic - My person favorite
    • LINQ to SQL - not yet, but I feel this should be on the list.
    • LLBLGen ($) - Not free, but I've heard many good things about it.
    • More
  • Continuous Integration - Simple process that sits on a server and waits for things to get checked into your source control system.  When it sees something new, it gets all of the latest source code, compiles it, and runs all of the unit tests.  If there are any problems anywhere in that process it alerts the users (email, desktop icon, wave red flag, etc). (3)
  • One Step Build - it is unbelievably cool to be able to click one button and have your entire project compile, run all tests, and possibly create an install in one step.  Personally, I do like it, but with the current set of tools this is one of the hardest parts to setup.

Highly thought of but non-essential practices

I'm sure I have missed some tools in the list, but it is the practices that are more important anyway.  But if you put a comment about any tool that I missed I will add it to the list.

(1) Source Safe was notably not in the list.  While I don't like it either, it is better than nothing.  But if your team is greater than 5 people or you have multiple people working on the same project you really should look at one of the other products on the list.

(2) Not a unit testing library, but a Mock Object library.  Pick a unit testing libarary (NUnit, MBUnit, XUnit), then pick a mock object library (Rhino Mocks, TypeMock, NMock) to use with it.

(3) I really need to talk about continuous integration more.  But it is one of those things that leads you down a path.  First you add source control, then you start adhering to separation of concerns, then you start unit testing and mocking more, then you add continuous integration, and then one step builds are all part of the mix.  One best practice leads to another, but they don't make a bit of sense without some of the prior pieces.

(4) If you are a Code Rush person instead of a ReSharper person -- carry on, you are in good company.

($) The cost money - not just beer, but may have free version versions as well.

Labels: , , , , , , , , , , ,

 October 17, 2007

 Testing for Exceptions with NUnit

I came across a testing situation where I had to:
a. Make sure an exception was thrown
b. After it was thrown, make sure nothing else happened.

This is what happens when you throw exceptions from SQL Stored Procedures.

The Problem

The standard way of checking for exceptions in NUnit are thus:
1. ExpectedException attribute on the test method
2. Have your own try/catch block in your test method.

The problem with option 1 is that no assertions can be checked after the exception. The code runs until the exception is thrown, then you are done. That isn't what I need.

The problem with option 2 is that it is ugly. Yes, I'm being a cry baby here. I think the try catch throws more code in the test than I think should be there.

My Solution

So I created a new method called ExpectException. Here is the code:



   1:  public T ExpectException<T>(MyMethod method) where T : Exception
   2:  {
   3:      try
   4:      {
   5:          method.Invoke();
   6:      }
   7:      catch(T e)
   8:      {
   9:          return e;
  10:      }
  11:      return null;
  12:  }


So now in my case, I have a stored procedure that throws an SqlException. I want to call the method and return the exception so I can check the exception for specific values (make sure the message is correct). Also, if any other exception is thrown, say a Exception -- not a SqlException, the test will fail.

Here is my new code:

   1:  SqlException e = ExpectException<SqlException>(delegate { ReplaceUnitCall(oldUnit, newUnit); });
   2:  Assert.IsNotNull(e);

So as long as you are familiar with anonymous delegates, this is a pretty nice solution -- to me.

Labels:

 May 25, 2007

 ReSharper and MBUnit

I accept that I'm turning into a ReSharper fan boy. I'm OK with that.

But, one feature that was missing was MBUnit support for ReSharper.
I've been wanting to play with MBUnit because it seems to have a fast growing community of supporters. Plus, it is still being actively developed, while NUnit seems to be stagnating.

Today I found my answer. There is an add-in for ReSharper that adds MBUnit support.
You can read about it on the ReSharper Support Web Site.

Also, MBUnit released version 2.4 RTM yesterday.

Labels: , ,

 May 24, 2007

 ReSharper Unit Test Templates

While I was preparing for my presentation on Rhino.Mocks, I make a couple of nice ReSharper template for creating new unit tests and files with, and without, Rhino.Mocks additions.

So, if you are using Rhino.Mocks and ReSharper, I hope these would be useful to you.

Enjoy.

  • Test Class Template.xml -- File Templates, allows you to create a new test class, one with Rhino.Mocks initialized for NUnit, and one simple unit test class for NUnit.
  • Test Method Templates.xml -- Live Templates, creates new test methods for Rhino.Mocks and and another for standard NUnit.

Labels: , ,

 March 28, 2007

 NAnt-NUnit error

Ever have a weird error and there is nothing on Google to help you? This was one of those problems.

We are using nant to run all of our builds, and using the NUnit2 for testing. Just to help out, nant has a task called nunit2.

Unfortunately, every time I ran our build I would recieve this error:
[loadtasks] Failure scanning "C:\Program Files\nant-0.85\nant-0.85\bin\NAnt.NUnit2Tasks.dll" for extensions. Unable to load one or more of the requested types. Retrieve the LoaderExceptions property for more information.
That could have been a bit more helpful.

Fortunatly Reflector was able to save the day. I was able to find the NAnt.NUnit2Tasks.dll on my file system (not to hard, the error message told me right where to find it), and loaded it into Reflector.

From there I checked the references of the dll. Two caught my eye. nunit.core and nunit.util. Still using resharper I tried to expand the references...Resharper could not find them. And they did not exist in the same directory as NAnt.NUnit2Tasks.dll. That meant that nant was looking for them in the GAC and they were not there!

A quick little script proved that to be true. It looked like this:
gacutil.exe /l nunit.util

I received this message:
The Global Assembly Cache contains the following assemblies:

Number of items = 0

OK, enough debugging, I ran the following two scripts to put those two dlls in the GAC and everything worked fine:

C:\Program Files\Microsoft SDKs\Windows\v6.0\Bin>gacutil.exe /i "C:\Program Files\NUnit-Net-2.0 2.2.8\bin\nunit.core.dll"

C:\Program Files\Microsoft SDKs\Windows\v6.0\Bin>gacutil.exe /i "C:\Program Files\NUnit-Net-2.0 2.2.8\bin\nunit.util.dll"

No thanks Google, I'll just fix it myself thank you very much.

Labels: ,

 January 23, 2007

 Adventures in Agile Development

I've started a new project, with a new team, under a new development methodology. And for the most part I am happy with that. We have a small team of two developers and an architect/manager/used to be coder, and we are going as agile as possible.

Now Agile development means a lot of different things to a lot of different people. It is so bad that saying that you are an agile shop can mean almost anything you want it to. So to alleviate some of the confusion, here is some of what we are doing.
  1. Daily Scrum. This is a 10-15 minute meeting every morning to talk about what is going on with your code. Where are we at, what are we trying to do.
  2. Test Driven development. This means having tests for as much as possible (NUnit in our case). Granted, you cannot test everything, but there are development patterns that allow you to test most of what you do. Chief among them is the MVP pattern (Model-View-Presenter). I'll talk more about this later.
  3. Frequent check-ins. It goes like this. Write a test for your code. Write the code. Make sure the test works. Check in. This means you are checking in up to four times per day. It also means you are less likely to get lost in all the things you are trying to do. And it means you need a source control system (we are using Subversion), and an automated build process (we are using NANT and Cruise Control).
  4. The customer is allowed to get the daily build. This way they can keep up to date with what you are doing.
  5. Now throw in an alphabet soup of other best-practice-somewhere technologies like Rhino Mocks, Spring, and NHibernate . You see there is a lot to learn.
Luckily, Agile and Test driven development have reached a critical mass such that there are now tools to help you with this process. I've already mentioned NUnit, NAnt, Subversion, and Cruise Control. But there is also the excellent Microsoft Team Systems that has all of those tools bundled together.

Another tool that wasn't listed that we are also using is ReSharper. Very cool tool with a lot of enhancements to Visual Studio.NET -- and a little bit of pain as well. ReSharper has an extremely large collection of shortcuts, so many that it actually overwrites a number of Visual Studio.NET shortcuts. But, it does have a feature to deal with that. Any time you his a shortcut that both ReSharper and Visual Studio.NET want, a dialog pops up asking which short-cut to use. I guess that is the best compromise. Anyway, for the side of ReSharper, the refactorings, and unit test integrations make it worth the cost of entry (a little over $100).

Now, this is enough for right now. I hate overly long rambling blog posts, and I'm afraid this will have to turn into a series. So, next time I'll write up what MVP, Spring, NUnit, and Rhino Mocks have to do with each other.

Labels: , , , , ,