exceptionz

Thoughts on Technology, Methodology and Programming.

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

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: