如何将现有程序集转换为 MS 单元测试程序集?

发布于 2024-09-05 14:51:51 字数 92 浏览 2 评论 0原文

在 Visual Studio 2010 Pro 中,如何轻松地将经典程序集转换为 ms 单元测试程序集?

.csproj 文件中有一个要激活的标志吗?

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 ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

始终不够爱げ你 2024-09-12 14:51:51

问题是测试项目被“标记”在项目文件上 - 您可以按照以下四个简单步骤将类库转换为测试项目:

  1. 卸载项目 (.prj) 文件,然后打开它进行更新。
  2. 将以下行添加到项目中
    C#:

    <前><代码><项目>;
    <财产组>
    <程序集名称>....
    {3AC096D0-A1C2-E12C-1390-A8335801FDAB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}

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

    将以

  3. 重新加载项目
  4. 运行(正在运行)测试

请注意,您需要手动添加对 Microsoft.VisualStudio.QualityTools.UnitTestFramework.dll 的引用,以便能够使用测试相关属性

更新:在即将推出的 MSTest V2 中,这将不再是必要的,因为 MSTest 成为一个 NuGet 包,其工作方式与 NUnit/XUnit 类似

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

旧人九事 2024-09-12 14:51:51

在 Visual Studio 2017 中,您可以将 nuget 包 MSTest.TestAdapterMSTest.TestFramework 添加到您的 C# 项目,然后使用 [TestClass][TestMethod] 属性。它们将被自动发现并列在测试资源管理器中。无需手动添加 ProjectTypeGuids 即可启用测试。

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

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);
        }
    }
}
小瓶盖 2024-09-12 14:51:51

单元测试项目只是类库,其中包含具有属性 [TestClass] 的类,并且 .csproj 文件没有任何标志。您可以将类添加到您的项目中并设置属性[TestClass],它将成为测试类。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文