30

In Visual Studio 2010 Pro, how can I easily convert a classic assembly to a ms unit test assembly ?

It there a flag to activate in the .csproj file ?

Patrice Pezillier
  • 4,476
  • 9
  • 40
  • 50
  • is there any chance you could actually accept the *correct* answer to this question? – Robert Harvey Sep 14 '16 at 22:51
  • @RobertHarvey which one do you class as correct? The answer currently marked as accepted (by Dror Helper) seems to work for me. – Tom Wright Jun 09 '17 at 16:35
  • Related post - [Convert C# Library To MSTest Project](https://stackoverflow.com/q/1185840/465053) – RBT Feb 24 '21 at 08:04

3 Answers3

53

The problem is that test projects are "marked" on the project file - you can convert a class library to Test project follow these four simple steps:

  1. Unload the project (.prj) file and then open it for update.
  2. add the following line to the project
    C#:

    <Project>
     <PropertyGroup>
      <AssemblyName>....</AssemblyName>
      <!-- add this line below -->
      <ProjectTypeGuids>{3AC096D0-A1C2-E12C-1390-A8335801FDAB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
     </PropertyGroup>
    </Project>
    

    VB - <ProjectTypeGuids>{3AC096D0-A1C2-E12C-1390-A8335801FDAB};{F184B08F-C81C-45F6-A57F- 5ABD9991F28F}</ProjectTypeGuids>

  3. Re-load the project back
  4. Run you (now working) tests

Note that you'll need to manually add reference to Microsoft.VisualStudio.QualityTools.UnitTestFramework.dll in order to be able to use test related attributes

Update: In the upcoming MSTest V2 this will not be nesessery as MSTest becomes a NuGet package which works just like NUnit/XUnit

Dror Helper
  • 30,292
  • 15
  • 80
  • 129
  • 1
    Ah, the clean intuitive interface of VS2010, what a wonderful solution! – John Oct 27 '14 at 15:23
  • For converting it back to a c# library, simply remove that line altogether. – womp Oct 23 '15 at 23:58
  • 1
    Hopefully the next MSTest version (V2) will make this obsolete and we could use simple class library https://blogs.msdn.microsoft.com/visualstudioalm/2016/06/17/taking-the-mstest-framework-forward-with-mstest-v2/ – Dror Helper Aug 22 '16 at 06:28
  • It may also be worth noting that you have to add a reference to `Microsoft.VisualStudio.QualityTools.UnitTestFramework.dll` for access to the required attributes. Also, works in VS 2017. – Marc L. Jan 03 '17 at 18:36
  • Keep in mind that the default "unit test" project in VS2017 is no longer a special project, instead it's a class Library with two NuGet packages – Dror Helper Jul 10 '18 at 11:17
0

In Visual Studio 2017 you can add nuget packages MSTest.TestAdapter and MSTest.TestFramework to your C# project and from then on use [TestClass] with [TestMethod] attributes. They will be automatically discovered and listed in the Test Explorer. A manual added ProjectTypeGuids is not needed to enable tests.

// MyFancyClassTests.cs

namespace MyTests
{
    using Microsoft.VisualStudio.TestTools.UnitTesting;

    [TestClass]
    public class MyFancyClassTest
    {
        private readonly MyFancyClass _MyFancyClass;

        public MyFancyClassTest()
        {
            _MyFancyClass= new MyFancyClass();
        }

        [TestMethod]
        public void MyFancyClass_UninitializedName_ReturnsEmptyString()
        {
            Assert.AreEqual(string.Empty, _MyFancyClass.Name);
        }
    }
}
VisorZ
  • 526
  • 1
  • 5
  • 15
-3

Unit Test Project is only Class Library which have classes with attribute [TestClass], and .csproj file doesn't have any flags. You can add class to your project and set attribute [TestClass] and it will be test class.

Pavel Belousov
  • 1,848
  • 2
  • 14
  • 22
  • Ok, but I want to start unit test in Visual Studio 2010. Even if I add a reference to Microsoft.VisualStudio.QualityTools.UnitTestFramework and in my class i use [TestClass] and [TestMethod] attribute, I can't view my methods in the Test View window in VS2010. – Patrice Pezillier Jun 10 '10 at 08:00
  • You might have to build the code first before the methods appear in the window. Simply adding the attributes to the code, and not building, does not update the test view window (I think!). – Jason Evans Jun 10 '10 at 08:05
  • 16
    I had the same issue. After adding a class with the [TestClass] attribute containing some methods with the [TestMethod] attribute and rebuilding, the tests did not appear in the Test View. Adding {3AC096D0-A1C2-E12C-1390-A8335801FDAB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} to the csproj file fixed it. See http://social.msdn.microsoft.com/forums/en-US/vststest/thread/2192fac0-0de7-45c6-872a-2be49e95ce2c/ – Teevus Aug 16 '11 at 02:03
  • 1
    Teevus your comment contains the best answer to this question. Maybe you should add it as a real answer so we can vote it up :) – Antony Perkov Feb 21 '12 at 23:15