Skip to content
Evgeny Safronov edited this page Feb 25, 2014 · 4 revisions

Why are you here?

#Motivation No motivation! Use at your own risk.

Introduction

It is no secret that any production code are preferred to be covered by tests. If you use TDD is your projects - there is no any problem with it, because your code is already covered as mush as it possible. But what if you started to work at project without any test code? Where is the guarantee that any code changing wouldn't lead to the broken project?

In this case some of responsible parts of code must be covered by tests to be sure that any refactoring would not broke it.

If you did use IDE like Intellij IDEA, MSVS or Eclipse, you might notice that pretty feature named 'CodeCoverage' tools or something like this. Standard usage is simple: you run code (tests) and your IDE is colorized with green or black lines pointing at some uncovered parts of code.

QtCreator has not that feature. That's why I decide to write it.

There is tool named Gcov that helps to track code coverage:

Gcov is a source code coverage analysis and statement-by-statement profiling tool. Gcov generates exact counts of the number of times each statement in a program is executed and annotates source code to add instrumentation. Gcov comes as a standard utility with GNU CC (GCC) suite

And frontend to it - lcov:

LCOV is a graphical front-end for GCC's coverage testing tool gcov. It collects gcov data for multiple source files and creates HTML pages containing the source code annotated with coverage information. It also adds overview pages for easy navigation within the file structure. LCOV supports statement, function and branch coverage measurement.

Okay, let's use it! Here the result: 1

How to build

At this moment only *nix are supported, sorry. Reason is - you have to compile your test project with gcc.

  1. Download QtCreator source files from here
  2. Unpack it in some directory. Let's name it QT_CREATOR_PATH
  3. Compile QtCreator. There shouldn't be any problem with it. Just:
cd QT_CREATOR_PATH
qmake -r
make
  1. Clone this repository in some directory. Let's name it PLUGIN_PATH
  2. Edit src/lib/lib.pro file. You need to change WORK_DIR variable to actually QtCreator source directory.
  3. Compile project:
cd PLUGIN_PATH
qmake -r
make
  1. Install lcov. If you use deb-like system, type sudo apt-get install lcov
  2. After that, run QtCreator from QT_CREATOR_PATH/bin/qtcreator
  3. There must be new green button at the left panel. Don't click on it yet.
  4. (Optional, but recommended) Read gcov and/or lcov start guide.
  5. Create your test subproject. Add compile flags to it:
LIBS += -lgcov
QMAKE_CXXFLAGS += -fprofile-arcs -ftest-coverage
  1. Compile and run test subproject. After that, activate colorizing through Tools/CodeCoverage/Show code coverage or type Ctrl+H
Clone this wiki locally