How to tell if a project is a test project?

There use to be a property for IProject in older SDK versions that would tell if a project was a test project (I'm basing this on old repos I've browsed in GitHub).

This property appears to have become obsolute. Maybe because VS now supports multiple test libraries.

Anyway, is there a recommended way to tell if a project is a test project? Should I just look at the assembly references?

2 comments
Comment actions Permalink

I'm not aware of any old APIs for this, but you can find if a project is a test project by checking IProject.ProjectProperties.ProjectTypeGuids and look for {3AC096D0-A1C2-E12C-1390-A8335801FDAB}. This is the guid that marks a project as a Visual Studio test project (see http://www.mztools.com/articles/2008/mz2008017.aspx about the ProjectTypeGuid element in the project file).

ReSharper provides a nicer API for working with this - IProjectFlavor and IProject.HasFlavour (yep, mixing the UK and US spellings, just to keep you on your toes). ReSharper provides a bunch of IProjectFlavor implementations, so it can easily check if you're in a sharepoint project, or web, windows phone, Xamarin, etc. It needs these to be able to modify its behaviour appropriately. Unfortunately, it doesn't do anything with test projects, so doesn't define an IProjectFlavor for them. So, stick with the guids.

Note that this will only show you projects that have been created by VS as a test project. It won't show you when a simple class library project has referenced nunit, xunit or mstest and is an "unofficial" test project. You'd need to detect references there.

0
Comment actions Permalink

Okay, this is great to know.

I think I'll go with a naming convention for finding test projects. I'll make it a configurable option for the plugin.

0

Please sign in to leave a comment.