user531571
user531571

Reputation: 146

Code coverage tool visual studio 2010 c++

does any know how to do some unit testing in c++ with the code coverage results working in visual studio 2010, I have looked for some answers everywhere. I want to keep the project I am testing and the testing project separate. Using a project the outputs a static lib is not a solution as the code coverage tool in VS 2010 can't put instrumentation code inside of the lib. Ive tried dll as the project to be tested, but then that cant link properly with test created due to the CLR:safe parameter being turned on for tests. Any ideas people? Or are MS just incapable of making a c++ code coverage tool.

Upvotes: 5

Views: 5430

Answers (2)

user531571
user531571

Reputation: 146

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

Upvotes: 0

Chris Schmich
Chris Schmich

Reputation: 29476

(Full disclosure: I'm on the team that maintains this feature)

Native C++ code coverage is supported by VS2010, but as you've seen, you can only instrument linked binaries (e.g. .dll or .exe). This means that the code you want to collect coverage for must be linked into a binary image before you instrument it.

What unit test framework are you using? It sounds like your test project is pure managed C++ (/clr:safe). If you build your native C++ project as a DLL, your test project should at least be able to call into the native DLL using P/Invoke calls. By doing this, you don't actually need to link your native .lib into your test project.

Upvotes: 6

Related Questions