How to convert an existing assembly to a ms unit test assembly?

耗尽温柔 提交于 2021-02-17 21:11:27

问题


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 ?


回答1:


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




回答2:


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);
        }
    }
}



回答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.



来源:https://stackoverflow.com/questions/3012571/how-to-convert-an-existing-assembly-to-a-ms-unit-test-assembly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!