【问题标题】:Visual C++ Team Test problemsVisual C++ 团队测试问题
【发布时间】:2009-11-22 21:13:29
【问题描述】:

我正在尝试为使用 Visual Studio 测试套件运行的本机 C++ 进行一些单元测试。我只有一个名为“Shape”的简单类。我按照教程做了以下步骤:

  • 为我要测试的原生类创建了一个名为“MShape”的“引用类”包装器
  • 将配置类型更改为 .dll
  • 将 CLR 支持更改为 /CLR
  • 将链接器“配置文件”设置为 /PROFILE
  • 重新编译成功
  • 添加了 Visual C++ 测试项目
  • 使用单元测试向导添加了新的单元测试
  • 在向导中,选择我要测试的方法

现在我有以下问题:

  • Visual Studio 报告大多数单元测试生成失败,因为“无法比较数组中的两个元素”
  • 当我尝试编译测试项目时,C++ 编译器崩溃。这行是罪魁祸首:

    MShape_Accessor^ shape = gcnew MShape_Accessor();

如果我右键单击并选择 Go to definition,VS 会说符号未定义。

这是 MShapeTest.cpp 的完整代码(由 Visual Studio 生成):

#include "StdAfx.h"
#include "StdAfx.h"
using namespace Microsoft::VisualStudio::TestTools::UnitTesting;
namespace TestProject1 {
    using namespace System;
    ref class MShapeTest;


    /// <summary>
///This is a test class for MShapeTest and is intended
///to contain all MShapeTest Unit Tests
///</summary>
 [TestClass]
 public ref class MShapeTest
 {

 private: Microsoft::VisualStudio::TestTools::UnitTesting::TestContext^  testContextInstance;
    /// <summary>
    ///Gets or sets the test context which provides
    ///information about and functionality for the current test run.
    ///</summary>
 public: property Microsoft::VisualStudio::TestTools::UnitTesting::TestContext^  TestContext
   {
    Microsoft::VisualStudio::TestTools::UnitTesting::TestContext^  get()
    {
     return testContextInstance;
    }
    System::Void set(Microsoft::VisualStudio::TestTools::UnitTesting::TestContext^  value)
    {
     testContextInstance = value;
    }
   }

#pragma region Additional test attributes
   // 
   //You can use the following additional attributes as you write your tests:
   //
   //Use ClassInitialize to run code before running the first test in the class
   //public: [ClassInitialize]
   //static System::Void MyClassInitialize(TestContext^  testContext)
   //{
   //}
   //
   //Use ClassCleanup to run code after all tests in a class have run
   //public: [ClassCleanup]
   //static System::Void MyClassCleanup()
   //{
   //}
   //
   //Use TestInitialize to run code before running each test
   //public: [TestInitialize]
   //System::Void MyTestInitialize()
   //{
   //}
   //
   //Use TestCleanup to run code after each test has run
   //public: [TestCleanup]
   //System::Void MyTestCleanup()
   //{
   //}
   //
#pragma endregion
 public: [TestMethod]
   [DeploymentItem(L"TP4.dll")]
   void MShapeConstructorTest()
   {
    MShape_Accessor^ shape = gcnew MShape_Accessor();

   }
 };
}
namespace TestProject1 {

}

我尝试的每次 VSTS 安装都会出现完全相同的问题。

【问题讨论】:

  • 我正在尝试使用托管的测试套件测试本机 C++ 类。这就是为什么我想我必须按照我的解释创建一个托管包装器。此处提供的代码来自测试项目,由 Visual Studio 生成。
  • 你能发布MShape_Accessor的定义吗?会不会是它是一个原生类,而您正试图将 gcnew 它作为一个托管类?
  • Shape 是原生类。 MShape 是 Shape 的“参考类”包装器。我猜,MShape_Accessor 是一个自动生成的类,用于在 Team Test 中使用 MShape。我应该使用它而不是 MShape,因为我不能直接实例化 MShape。我无法访问它的定义,甚至不能使用 Go To Definition,因为 VS 说符号未定义。

标签: c++ visual-studio unit-testing


【解决方案1】:

我刚刚使用 MS VS Test 设置了一个简单的测试测试,我可以让它运行。这是项目:

http://www.somethingorothersoft.com/TestTest.zip

我猜你遇到的任何问题都与 MShape 的定义有关。

或者,您可以直接在测试中测试您的非托管代码。您需要将测试项目的 CLR 支持从 /CLR:Safe 更改为 /CLR,然后在测试中直接运行 C++。

我尝试在演示中包含该功能,但我无法让这两种功能在同一个项目中运行 - 即,既使用托管包装器,又使用不使用同一目标项目的包装器。如果您将 undertest 项目设为静态库并支持远程 CLR,您将能够在您的测试项目中运行未管理的代码。

【讨论】:

  • 谢谢。我用一个新项目试了一下,它奏效了。我不确定第一个项目有什么问题。感谢您提出直接测试非托管代码的建议,这可能会使它更容易。