5

I have a classical SWIG setup where a bunch of C++ function are called from Python. Now, after adding some tests (in Python), I'd like to get test coverage on the C++ sources. The classical Python method is

nosetests --with-coverage --cover-package=mypackage

but that only works for native Python modules. In fact, this will return the coverage on the mypackage.py file generated by SWIG.

Since I need coverage on the C++ files (/the shared library generated by SWIG), adding --coverage to compile and linker flags is certainly called for. Not sure where to go from there though.

Any hints?

Nico Schlömer
  • 53,797
  • 27
  • 201
  • 249
  • Just curious. Usually, you would have a library, sometimes without source code. Wouldn't it make more sense to test this library and think of test coverage for the library rather than the wrapper. Next, using `nosetests` can then be used to return the coverage of the wrapper. – Jens Munk Aug 22 '16 at 19:47
  • I could perhaps build the library and write tests in C++ to get the coverage from that. This'll be a lot more tedious though since I don't have certain key tools available there that I do have in Python. – Nico Schlömer Aug 22 '16 at 20:22
  • You could also make covering test with the only purpose of testing semantics and error handling. Next, use python for validating against a reference implementation. – Jens Munk Aug 22 '16 at 21:55

1 Answers1

0

Faced with a similar issue, but with C code. It's not related to the SWIG part, just need to compile your C++ code with the coverage instrumentation and execute it. I used gcov and gcovr for that.

With gcov, to make this work, compile the code with the following flags (enables gcov instrumentation)

CFLAGS_VAL += -O0 --coverage

Then, execute the test. Following the test run, .gcno and .gcda files should be generated.

To create the coverage report, run from your root folder

gcovr -r . --filter="" --html --html-details -o coverage/coverage.html

GCOV docs, here

Same can create with lcov, follow an example in this wiki page

alexpov
  • 1,158
  • 2
  • 16
  • 29