Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compiling googletest for gcov

I want to get coverage information from my googletest tests, but I'm having trouble finding good instructions.

I presume I'm supposed to compile my gtest binary such that it spits out .gcno and .gcna files. However no combination of compiler flags seems to get this to happen.

I have tried using --coverage and -fprofile-arcs -ftest-coverage, both on compilation and linking, but to no avail.

Am I just mistaken in the whole approach? Will googletest tests ever compile like this?

For completeness here is the whole makefile:

# -*- indent-tabs-mode:t; -*-

## Vars
CXX=g++
BDDOBJ=../obj
OBDD_DIR=../src
OBDD_INCLUDE=-I$(OBDD_DIR)
#FLAGS=-Wfatal-errors -I./gtest-1.7.0/include  -L./gtest-1.7.0/lib/.libs -lgtest -lgtest_main -lpthread

GTEST_DIR=./gtest-1.7.0
SRCDIR=./src
OBJDIR=./obj
BINDIR=./bin
LIBDIR=./lib
VPATH=$(SRCDIR) $(OBJDIR) $(LIBDIR) $(BINDIR)
COVERAGE=-O0 -g --coverage

MKDIR=mkdir -p
RM=rm -rf

.PHONY: clean
.INTERMEDIATE: default $(BINDIR) $(OBJDIR) $(LIBDIR)

default: obddtest

#Binary
obddtest: $(BINDIR) libgtest.a Vertex.o Vertex_unittest.o Edge.o Edge_unittest.o Graph.o Graph_unittest.o main.o 
    g++ $(COVERAGE) -isystem ${GTEST_DIR}/include $(OBDD_INCLUDE) -pthread \
    $(OBJDIR)/Vertex.o \
    $(OBJDIR)/Vertex_unittest.o \
    $(OBJDIR)/Graph_unittest.o \
    $(OBJDIR)/Edge.o \
    $(OBJDIR)/Edge_unittest.o \
    $(OBJDIR)/Graph.o \
    $(OBJDIR)/main.o \
    $(LIBDIR)/libgtest.a \
        -o $(BINDIR)/obddtest

## Main
main.o: main.cc 
    g++ $(COVERAGE) -isystem ${GTEST_DIR}/include $(OBDD_INCLUDE) -pthread -c $< -o $(OBJDIR)/$@

## gtest library
gtest-all.o: $(OBJDIR)
    g++ $(COVERAGE) -isystem ${GTEST_DIR}/include -I${GTEST_DIR} -pthread -c ${GTEST_DIR}/src/gtest-all.cc -o $(OBJDIR)/gtest-all.o

libgtest.a: $(LIBDIR) gtest-all.o
    ar -rv $(LIBDIR)/libgtest.a $(OBJDIR)/gtest-all.o

## Source under test
%.o: $(OBDD_DIR)/%.cpp $(OBJDIR) 
    g++ $(COVERAGE) -fPIC -O0 $(OBDD_INCLUDE) -c $< -o  $(OBJDIR)/$@

## Tests
%_unittest.o: %_unittest.cc $(OBJDIR) 
    g++ $(COVERAGE) -isystem ${GTEST_DIR}/include $(OBDD_INCLUDE) -pthread -DTESTDATA=\"$(CURDIR)/data/\" -c $< -o $(OBJDIR)/$@

## Housekeeping
$(LIBDIR):
    $(MKDIR) $(LIBDIR)

$(OBJDIR):
    $(MKDIR) $(OBJDIR)

$(BINDIR):
    $(MKDIR) $(BINDIR)

clean:
    $(RM) $(LIBDIR) $(OBJDIR) $(BINDIR)
like image 806
jsj Avatar asked Dec 26 '13 07:12

jsj


1 Answers

You have to add --coverage on both compilation and linking, as you are already correctly doing. This generates bytecode that will output coverage information when visited. To generate all the files that gcov will examine, you now simply need to run the program (or the test program generated by googletest). After running it once (and only once because if you run it multiple times it will add to the observed data), you will see the files generated and you can now call into gcov.

It seems, from your (correct) makefile that you're just missing the "running the test program" step.

like image 170
Carneiro Avatar answered Sep 29 '22 11:09

Carneiro