25

I am using gtest for testing my code in C++ with Visual studio 2010. But I could not able to makeout that I have reached 100% code coverage. To make sure that I have covered 100% code coverage, I would like to know that, is there any way to find out the code coverage gtest or not? Because I have Googled a lot but I did not find any possible way to get the code coverage result by using gtest in Windows enviornment. If it is possible please let me know.

Thanks A Lot..

Rasmi Ranjan Nayak
  • 11,510
  • 29
  • 82
  • 122
  • 3
    Why `-ve` vote?? I really did not find any solution for this, that is why I put the question in this portal. Try to understand – Rasmi Ranjan Nayak Aug 19 '13 at 06:22
  • You'll need to use the coverage tools of your compiler. It has nothing to do with the testing framework (gtest, cppunit, etc.). – πάντα ῥεῖ Aug 19 '13 at 06:44
  • @Rasmi Ranjan Nayak: I once used NCover for .Net code coverage. Also used gtest for C++. But not sure any tool about C++ code coverage. – Pranit Kothari Aug 19 '13 at 07:25
  • @RasmiRanjanNayak: +1. Not deserving downvotes for this question. – Pranit Kothari Aug 19 '13 at 07:26
  • Also remember that code coverage tools are far from perfect. They only look at what code is executed, and have no way of finding out what code is actually tested. So you can have lots of code that are executed during the tests, but which the assert does not depend upon. – martiert Aug 19 '13 at 07:30
  • @RasmiRanjanNayak Please clear me if I am wrong, you need code coverage for your source code not for test cases? Because if you are using it with test cases, coverage will be significantly low. – Pranit Kothari Aug 19 '13 at 07:32
  • @RasmiRanjanNayak: Try this. http://stackoverflow.com/questions/1161832/free-testing-code-coverage-systems-for-c?rq=1 – Pranit Kothari Aug 19 '13 at 07:38
  • @pranitkothari: Thanks for your reply. But I am looking for Opensource or freeware which I can use in VS 2010 – Rasmi Ranjan Nayak Aug 19 '13 at 07:42
  • @pranitkothari: I am looking for my test cases. Which can check wheater the test cases are covering all the functionalities of the code or not. – Rasmi Ranjan Nayak Aug 19 '13 at 07:45
  • 1
    @pranitkothari why should the coverage of test cases be low? If you execute all tests, there should be approx. 100% test case coverage. But of course the goal is to get 100% application code coverage. – Arne Mertz Aug 19 '13 at 07:54
  • @ArneMertz +1: Ya true. If all test cases written properly to cover all parts of code. Coverage should come 100%. – Pranit Kothari Aug 19 '13 at 08:06
  • @Arne Mertz: You are right. But there should be one tool which will tell you that, the test cases are covering 100% of code coverage. Reply me if I am wrong – Rasmi Ranjan Nayak Aug 19 '13 at 08:36

2 Answers2

42

You can try OpenCppCoverage: https://github.com/OpenCppCoverage/OpenCppCoverage.

It is open source, designed for Visual Studio C++ and works well with Google Test. I already used successfully for mid size project (~500 000 lines of code).

Hope that help.

Disclaimer: I am the author of the tool.

Shaac
  • 195
  • 14
OpenCppCoverage
  • 551
  • 5
  • 4
  • Will it work for `Visual Studio 2010 Ultimate/Professional Edition`? We have `Windows-7` and `Windows-Vista OS`.. Let me know the system dependancy – Rasmi Ranjan Nayak Sep 03 '14 at 08:52
5

Code coverage in C++ can not be handled by the testing framework solely, because a coverage analysis tool has to know the whole extent of the code (wich the testing framework has not) and it has to instrument the code under test somehow to monitor wich parts of the code get executed.

I had the same desire like you once, wanting to measure my test coverage in MSVC. This is what I learned:
MSVC ships with some command line tools for these instrumentations, googling a bit will get you one or two msdn blog posts about how to use them. Frankly, its not very convenient and easy to use. If you look for third party tools, you will probably not find any free ones. Any tools I found at all were enterprise tools with license fees in the range of several hundred to more than a thousand dollars, so not really an option if you are not a company.

Arne Mertz
  • 24,171
  • 3
  • 51
  • 90