首页 文章

代码覆盖工具visual studio 2010 c

提问于
浏览
4

有没有人知道如何使用Visual Studio 2010中的代码覆盖率结果在c中进行一些单元测试,我到处寻找一些答案 . 我想保持我正在测试的项目和测试项目分开 . 使用项目输出静态库不是解决方案,因为VS 2010中的代码覆盖工具无法将检测代码放在lib中 . 我已经尝试了dll作为要测试的项目,但是然后由于CLR创建了测试而无法正确链接:安全参数被打开以进行测试 . 人们有什么想法?或者MS只是无法制作c代码覆盖工具 .

2 回答

  • 6

    (完全披露:我是维护此功能的团队)

    VS2010支持本机C代码覆盖,但正如您所见,您只能检测链接的二进制文件(例如.dll或.exe) . 这意味着您要收集覆盖范围的代码必须在检测之前链接到二进制图像 .

    你使用什么单元测试框架?听起来你的测试项目是纯粹的托管C( /clr:safe ) . 如果您将本机C项目构建为DLL,则您的测试项目至少应该能够使用P/Invoke调用调用本机DLL . 通过这样做,您实际上不需要将本机.lib链接到测试项目中 .

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

相关问题