Current AppDomain’s Base Directory issue while running Unit Tests in VS2008


As I mentioned earlier, we are currently converting all the solutions of our application from Visual Studio 2005 to Visual Studio 2008. While converting solutions to VS2008 we faced some minor issues. One of them were related to Unit Test in VS2008 which I blogged earlier. The current issue also is related to Unit Test in Visual Studio 2008. Here’s the details –

 

Background & Issue:

 After converting a VS2005 solution to VS2008 solution all the unit test cases that reference current AppDomain’s Base Directory property (AppDomain.CurrentDomain.BaseDirectory) fails.

 

 

Cause of Issue:

 This happens as in Visual Studio 2008, while running under the context of Unit Test current AppDomain’s Base Directory property (AppDomain.CurrentDomain.BaseDirectory) points to Visual Studio 9.0 IDE directory (typically – C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\)

 

 

This behavior is different and incorrect in Visual Studio 2008 from Visual Studio 2005 and previous versions where current AppDomain’s Base Directory property would correctly points to Applications Base directory (if not set otherwise explicitly).

 

Solution:

To fix to issue do the following steps –

 

  1. In the class of the test method where current AppDomain’s Base Directory property (AppDomain.CurrentDomain.BaseDirectory) is used, add a ClassInitialize method for the unit test.
     
  2. Add the following code in the ClassInitialize method to initialize the AppBase to Application’s Current Directory –

      [ClassInitialize]

        public static void ClassInitialize(TestContext testContext)

        {

            AppDomain.CurrentDomain.SetData("APPBASE", Environment.CurrentDirectory);

     }

 

Now current AppDomain’s Base Directory property (AppDomain.CurrentDomain.BaseDirectory) even while running within the Unit Test context of VS2008 would refer to Application’s Current Directory –

 

 

 

Note:

Microsoft has recognized this as a bug in Visual Studio 2008. It’s possible that this behavior may change back to original behavior as in 2005, however if this was to happen it would not be done until SP1 for Orcas 2008 and no time frame on SP1.

 

http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=2622623&SiteID=1

http://www.codeplex.com/entlib/Thread/View.aspx?ThreadId=15580

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