Saturday, August 7, 2010

How to change an existing Visual Studio project into a test project

If you prefer to have your tests in the same Visual Studio project as the source code you are testing, then in order to be able to run your tests the project must be modified to be a test project. This is how to do it:

1) Choose Add reference on the project, and add a reference to "Microsoft.VisualStudio.QualityTools.UnitTestFramework".

2) Edit the project file by right clicking it and choosing "Unload Project". Then right click again and choose Edit. Notepad can optionally be used for this.

3) In the first <PropertyGroup> section, add the following element:

<ProjectTypeGuids>{3AC096D0-A1C2-E12C-1390-A8335801FDAB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>

The first GUID is the project type identifier for a test project, the second is the identifier for a C# class library. If your project is of any other type, replace the second GUID with the correct project type GUID defined here.

4) Save and choose Reload project.

You should now be able to run your tests through Visual Studio.