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.

 

2 Comments

Comments have been disabled for this content.