I’m working on a high-profile project right now that uses some pretty cool advanced frameworks and tools in the business. Can’t reveal too much but here’s a lil’ list.

  • Windsor Container
  • Inversion of Control / Dependency Injection
  • LINQ (to SQL)
  • Continuous Integration (using Team Build)
  • ASP.NET MVC framework
  • Rhino Mocks
  • VS2008 Team Edition for Devs
  • MSTest (unit tests)

Jealous much? ;)

Anyways, so i was working on this unit test case to make some minor changes and after i was done, i tried running it by right clicking in the code window, and hoping to click on the “Run Tests” option in the context menu. Here’s what i was hoping to see.

Context menu with

But i didn’t see that option. Hmm….time for a little investigation.

So how does Visual Studio know when to provide “Run tests” option in the right click menu? How does it know not to display it in a ASPX file or any other normal class file?

There must be some kind of identifier it uses to recognize a file/class as a test class and hence enable the option in such cases.

The test class files are C# files just like any other files in the solution and so you probably are not going to find a lot of differences comparing a test file with a non-test file besides the code it holds, of course.

So the next step is to compare the projects that hold these files. Let’s compare a non-test project and a test project. (I used ExamDiff to compare the .csproj files)

And here’s the result of the comparison. The one on the right is the test project (click on the image below for better resolution).

difference between a test and non-test project files

See where the red arrow points? The test project has a ProjectTypeGuid as shown below.

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

So that’s what differentiates a test project from a non-test project.

And that’s how Visual studio understands whether or not to show the “Run tests” option in the right click context menu for a class under a project.

Coming back to our oriignal problem, why was it missing in the class i was working on? The developer that created that project did not create it as a “test” project – instead he chose the “class library” option in the create new project dialog.

Fixing the problem was easy. Open the csproj file in some text editor and add the ProjectTypeGuids element where it belongs and you are done!

I feel like Sherlock Holmes sometimes….

Leave a Reply