AppDomain.CurrentDomain.BaseDirectory Changed in Unit Tests with Visual Studio 2008

Recently I tried converting a solution from Visual Studio 2005 to 2008. Everything seemed to work until I ran all the unit tests. More than 50% of them failed.

I found the reason is because AppDomain.CurrentDomain.BaseDirectory changed from 2005 to 2008. In 2005 the value returned was the projects test results directory. In 2008, it returns the directory that the executing assembly of the unit test is located. i.e. C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\.

The fix for this problem is create a base class that all unit tests inherit from. In the base class add this code

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

This sets the BaseDirectory to the CurrentDirectory, which is expected test results directory.

I couldn't find any information on this, so I hope this helps others running into this issue.

 

Published Wednesday, January 23, 2008 2:57 PM by narshe
Filed under: , ,

Comments

# AppDomain.CurrentDomain.BaseDirectory Changed in Unit Tests with Visual Studio 2008 - Josh Close

Pingback from  AppDomain.CurrentDomain.BaseDirectory Changed in Unit Tests with Visual Studio 2008 - Josh Close

# re: AppDomain.CurrentDomain.BaseDirectory Changed in Unit Tests with Visual Studio 2008

Wednesday, February 11, 2009 2:14 AM by .Net Developer

Thank you so much.. you saved a bunch of developer time.

# re: AppDomain.CurrentDomain.BaseDirectory Changed in Unit Tests with Visual Studio 2008

Tuesday, January 12, 2010 6:28 PM by DanB

Building upon the information in your post (hugely helpful, btw),

If you upgrade to VS-2k8-SP1, the AppDomain.CurrentDomain.BaseDirectory goes back to the testing directory.

Leave a Comment

(required) 
(required) 
(optional)
(required)