exceptionz

Thoughts on Technology, Methodology and Programming.

Archive for March, 2006

VS2005 Unit Testing Problems

Posted by Marcus Wyatt on 27 March 2006

I’ve been using VS2005 Team developer on a commercial project for the past 5 months and the amount of issue’s I’ve found with things like the Testing framework and some other IDE oddities, has made me seriously conceder returning to NUnit as a testing framework again. I still feel NUnit is superior as a framework, it might not have the data driven test or the whiz-bang CollectionAssert objects, but for doing TDD it is still the best.

Today my VS IDE stopped executing the test for some reason. When you run the test inside the test view window, the IDE display the test result window with the test appearing as pending. Then suddenly the tests turn into aborted. Why? Beats me… I’ll try to fix the problem tomorrow.

So what problems did I find with the VS2005 Testing framework:

It doesn’t handle inheritance at all – here is a code snippet explaining what I mean: 

[TestClass]
public abstract class ConnectionTest
{
   [TestInitialize]
   public void SetupConnection()
   {
   }
   [TestMethod]
   public void TestConnectionOpen()
   {
   }
}
 
[TestClass]
public abstract class TransactionTest : ConnectionTest
{
   [TestInitialize]
   public void BeginTransaction()
   {
   }
 
   public abstract void InsertDomainData();
 
   [TestCleanup]
   public void RollbackTransaction()
   {
   }
}
 
[TestClass]
public class DomainObjectTest : TransactionTest
{
   [TestInitialize]
   public void Setup()
   {
   }
   public override void InsertDomainData()
   {
   }
   
   [TestCleanup]
   public void TearDown()
   {
   }
   
   [TestMethod]
   public void Test()
   {
      Assert.IsTrue( true );
   }
}

James Newkirk used this approach in his book Test-Driven Development in Microsoft .NET to enable you to test Data access code. You create a connection then test the connection is open, then on the derived TransactionTest you create a new transaction and call the InsertDomainObject method that is abstract. On the DomainObjectTest fixture, the insert method is implemented and you save some data to the database. This is inside a transaction, so now you can perform your test. When the test is completed, the TearDown get’s called, which will roll the tranaction back, causing the database to return to it’s original state.

In this example VS2005 will sqeal with the following message “UTA018: DataAccessLayerTest.DomainObjectTest: Cannot define more than one method with the TestInitialize attribute.”. This is a bit of a problem as you normally would structure your tests using standard OO techniques like above. I’ve got quite few of these inheritance issue examples. So how do we solve this issue? In a very ugly way, here is the code: 

[TestClass]
public abstract class ConnectionTest
{
   public void SetupConnection()
   {
   }
   
   [TestMethod]
   public void TestConnectionOpen()
   {
   }
}
 
[TestClass]
public abstract class TransactionTest : ConnectionTest
{
   public void BeginTransaction()
   {
      base.SetupConnection();
   }
   
   public abstract void InsertDomainData();
   
   public void RollbackTransaction()
   {
   }
}
 
[TestClass]
public class DomainObjectTest : TransactionTest
{
   [TestInitialize]
   public void Setup()
   {
      base.BeginTransaction();
   }
   
   public override void InsertDomainData()
   {
   }
   
   [TestCleanup]
   public void TearDown()
   {
      base.RollbackTransaction();
   }
   
   [TestMethod]
   public void Test()
   {
      Assert.IsTrue( true );
   }
}

You have to explicitly call base.[METHOD] to get the inherited methods to be called. A bit of a problem in that you can’t just decorate the methods with the Initialize and Cleanup attributes. Painful….

Here you can find another issue with Assert.AreEqual.

tags: , , , ,

Currently listening to: *** 20. Anthony Pappa – Cass – Winamp ***

Advertisements

Posted in Development, TDD | Leave a Comment »

WSCF as part of the build process

Posted by Marcus Wyatt on 6 March 2006

Thinktecture has recently released version 0.6 of their new web service contract first add-in for Visual Studio 2005. For more information on what contract first SOA means for you as the developer, have a look at this nice introduction on thinktecture’s web site.

At the moment I’m working on a service oriented solution for a big retail chain (here is another post about some issues on the project) and one of the problems we face before we can get a continuous integration system going, is the ability to update web references as part of the build process.

WSCF offers a command line interface to the code generation engine (wscf.exe). All code generation can thus be included in your batch scripts and/or build process. The following screenshot shows all available options of wscf.exe.

tags: , , ,

Currently listening to: Deep Dish – Toronto disc 1 – Winamp *** 103. Deep Dish – Toronto disc 1

Posted in Development, SOA | Leave a Comment »