有谁知道如何在c++中做一些单元测试,并且代码覆盖率结果在visual Studio2010中有效,我到处寻找一些答案。我想让我正在测试的项目和测试项目分开。使用项目输出静态库不是一个解决方案,因为VS2010中的代码覆盖率工具不能将插装代码放在库中。我已经尝试将dll作为要测试的项目,但由于为测试打开了CLR:safe参数,因此无法与创建的测试正确链接。大家有什么想法吗?或者MS只是不能制作一个c++代码覆盖工具。
发布于 2011-01-20 15:16:44
(完全披露:我在维护此功能的团队中)
VS2010支持原生C++代码覆盖率,但正如您所看到的,您只能插入链接的二进制文件(例如.dll或.exe)。这意味着您想要收集其覆盖率的代码必须链接到二进制图像中,然后才能对其进行检测。
您使用的是什么单元测试框架?听起来您的测试项目是纯托管C++ (/clr:safe)。如果将本机C++项目构建为动态链接库,则测试项目至少应该能够使用P/Invoke调用调入本机动态链接库。通过这样做,您实际上不需要将本机.lib链接到测试项目中。
发布于 2011-01-21 07:10:46
//MyTestfile
#include "stdafx.h"
#include "MathFuncsDll.h"
using namespace System;
using namespace System::Text;
using namespace System::Collections::Generic;
using namespace Microsoft::VisualStudio::TestTools::UnitTesting;
namespace anothertest
{
[TestClass]
public ref class cuttwotest
{
public:
[TestMethod]
void TestMethod1()
{
Assert::AreEqual ((MathFuncs::MyMathFuncs::Add(2,3)), 6, 0.05);
}
};
}https://stackoverflow.com/questions/4743945
复制相似问题