[TIP] Gcov and py.test

Noufal Ibrahim KV noufal at nibrahim.net.in
Sat Dec 26 22:20:21 PST 2015

Hello everyone,
      I've been working on a C library as a side project and decided to
write tests for using Python. I exposed all the APIs I needed using CFFI
and then wrote tests in python which I ran using py.test. The library is
a "port" of psutil[1] into C and my tests just tested against the ouptut
of corresponding calls in psutil.
      This worked very well. You can see some of the tests here[2]. I
got a little more ambitious and decided to try to measure test coverage
using gcov[3]. I compiled the library with necessary options and wrote a
tiny py.test plugin[4] that runs gcov after the test is over and summarises
the results as a percentage. This worked very nicely as well but then
there was a problem which messed things up a bit. 

      The way gcov apparently works is by dumping the .gcda (coverage
database) file *after* the process is complete. This means that while
running py.test, the database is not created and I can't print the
summaries at the end of the test (like the pytest_cov plugin does for
python tests). I searched around a little and apparently, there is a
function that can be called which forces the dump[5]. I tried doing this
as part of the plugin but it didn't work. I'm guessing it's because the
plugin and the library I'm testing are different libraries and Gcov
understands that. My final way was to include a __gcov_flush call inside
my own library and then calling that using a py.test funcarg. It worked
and helped my own development quite a bit but is really ugly. 

      I'm sending this email to fish for ideas and to ask if someone
else has done something similar to this and how they've handled the


[1]  http://pythonhosted.org/psutil/
[2]  https://github.com/nibrahim/cpslib/blob/master/tests/test_memory.py
[3]  https://gcc.gnu.org/onlinedocs/gcc/Gcov.html
[4]  https://github.com/nibrahim/pytest_gcov
[5]  https://www.osadl.org/Dumping-gcov-data-at-runtime-simple-ex.online-coverage-analysis.0.html#Example_hello.c


More information about the testing-in-python mailing list