Search code examples
makefilegcov

How to recognize < x % coverage in gcov in Makefile?


I'm trying to define a make target which asserts a test coverage of x%, i.e. fails in case a file has < x% test coverage, but I don't seem to find a control of the returncode of gcov and I'd like to avoid parsing its output.

My make target currently is

GCOV = gcov

coverage:
    $(MAKE) CFLAGS='-g -O0 --coverage'
    $(MAKE) check
    cd src && $(GCOV) -o .libs/ *.c # there're no .gcno files in lib which means that gcov can't be run on them; gcov only seems to work in the current directory

I'm using gcov 4.9.2 on Ubuntu 15.04.


Solution

  • Gcov doesn't support this, but you can use a gcov wrapper like gcovr (version 3.4 or later). If you run

    $ gcovr --fail-under-line 80
    

    and have less than 80% line coverage in your project, then the gcovr command will exit with nonzero status (see the documentation for details). There's an analogous --fail-under-branch option to require a minimum branch coverage. You can use a --filter regex to select only those files that you are interested in.