【发布时间】: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