How can I get code coverage using gtest on Windows?

14,809

Solution 1

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.

Solution 2

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.

Share:
14,809

Related videos on Youtube

Rasmi Ranjan Nayak
Author by

Rasmi Ranjan Nayak

Always ready for programming. Favorite Programming Languages C, C++, Python, Java, Android

Updated on June 04, 2022

Comments

  • Rasmi Ranjan Nayak
    Rasmi Ranjan Nayak almost 2 years

    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
      Rasmi Ranjan Nayak over 10 years
      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
    • πάντα ῥεῖ
      πάντα ῥεῖ over 10 years
      You'll need to use the coverage tools of your compiler. It has nothing to do with the testing framework (gtest, cppunit, etc.).
    • Pranit Kothari
      Pranit Kothari over 10 years
      @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
      Pranit Kothari over 10 years
      @RasmiRanjanNayak: +1. Not deserving downvotes for this question.
    • martiert
      martiert over 10 years
      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.
    • Pranit Kothari
      Pranit Kothari over 10 years
      @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
      Pranit Kothari over 10 years
      @RasmiRanjanNayak: Try this. stackoverflow.com/questions/1161832/…
    • Rasmi Ranjan Nayak
      Rasmi Ranjan Nayak over 10 years
      @pranitkothari: Thanks for your reply. But I am looking for Opensource or freeware which I can use in VS 2010
    • Rasmi Ranjan Nayak
      Rasmi Ranjan Nayak over 10 years
      @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.
    • Arne Mertz
      Arne Mertz over 10 years
      @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.
    • Pranit Kothari
      Pranit Kothari over 10 years
      @ArneMertz +1: Ya true. If all test cases written properly to cover all parts of code. Coverage should come 100%.
    • Rasmi Ranjan Nayak
      Rasmi Ranjan Nayak over 10 years
      @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
    Rasmi Ranjan Nayak over 9 years
    Will it work for Visual Studio 2010 Ultimate/Professional Edition? We have Windows-7 and Windows-Vista OS.. Let me know the system dependancy